Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 70 additions & 6 deletions AnkiDraw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,14 @@ def blackboard_css():
</style>"""

def blackboard_js():
config = mw.addonManager.getConfig(__name__)
start_vis = str(config.get('start_visible', False)).lower()
start_pen = str(config.get('default_pen_index', 1))

return u"""
<script>
// Set from python qt ui
var visible = """ + ts_default_VISIBILITY + """;
var visible = """ + start_vis + """;
var perfectFreehand = """ + ts_default_PerfFreehand +""";
var pressureSensitivity = """ + str(ts_pressure_sensitivity).lower() + """;
var small_canvas = """ + str(ts_default_small_canvas).lower() + """;
Expand All @@ -389,9 +393,29 @@ def blackboard_js():
var fontSize = """ + str(ts_font_size) + """;
var fontBold = """ + str(ts_font_bold).lower() + """;
var fontItalic = """ + str(ts_font_italic).lower() + """;
var activePenIndex = 0;
var activePenIndex = """ + start_pen + """;
var convertDotStrokes = true

function forceShowCanvas() {
if (visible === false) { // Only change if it's currently false
visible = true;
canvas.style.display='block';
secondary_canvas.style.display='block';
if (ts_visibility_button) ts_visibility_button.className = 'active';
if (optionBar) optionBar.className = '';
}
}

function forceHideCanvas() {
if (visible === true) { // Only change if it's currently true
visible = false;
canvas.style.display='none';
secondary_canvas.style.display='none';
if (ts_visibility_button) ts_visibility_button.className = '';
if (optionBar) optionBar.className = 'touch_disable';
}
}

function getPenColorAndWidthByIndex(index){
switch (index) {
case 0:
Expand Down Expand Up @@ -461,6 +485,18 @@ def blackboard_js():
var ts_switch_pen4_button_path = document.querySelector('#ts_switch_pen4_button > svg > path');

// Arrays to save point values from strokes
if (!visible) {
canvas.style.display = 'none';
secondary_canvas.style.display = 'none';

if (ts_visibility_button) {
ts_visibility_button.className = '';
}

if (optionBar) {
optionBar.className = 'touch_disable';
}
}
var stroke_cache = [ ];
var lineHistory = [ ] // contains history of currentAction Items, defined below
var redoStack = [ ]
Expand Down Expand Up @@ -684,10 +720,9 @@ def blackboard_js():
function switch_visibility()
{
stop_drawing();
if (visible)
{
if (!visible) {
canvas.style.display='none';
secondary_canvas.style.display=canvas.style.display;
secondary_canvas.style.display='none';
ts_visibility_button.className = '';
optionBar.className = 'touch_disable';
}
Expand Down Expand Up @@ -2304,4 +2339,33 @@ def ts_onload():
addHook("showAnswer", resize_js)
ts_setup_menu()

ts_onload()
ts_onload()

from aqt.qt import QTimer

def apply_visibility_logic():
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this work for people who would like to keep the current behaviour, it looks like apply_visibility_logic will run even if start_visible is true and disable the visibility if no tag is present

"""
Checks for the 'draw' tag and directly calls the
appropriate JavaScript function to show or hide the canvas.
"""
if not mw.reviewer.card or not mw.reviewer.web:
return

target_tag = "draw"

note_tags = [t.lower() for t in mw.reviewer.card.note().tags]
should_show = target_tag.lower() in note_tags

if should_show:
mw.reviewer.web.eval("forceShowCanvas();")
else:
mw.reviewer.web.eval("forceHideCanvas();")

def check_for_draw_tag_wrapper(*args):
"""
Wrapper to handle hook arguments and add a small delay
to ensure the card's web content is fully loaded.
"""
QTimer.singleShot(100, apply_visibility_logic)

addHook("showQuestion", check_for_draw_tag_wrapper)
4 changes: 4 additions & 0 deletions AnkiDraw/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"start_visible": true,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the separate config.json here is not correct as mentioned in the previous comment

"default_pen_index": 0
}
7 changes: 7 additions & 0 deletions AnkiDraw/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# AnkiDraw Configuration

**start_visible**
If enabled, the drawing canvas opens automatically when reviewing a deck.

**default_pen_index**
Choose which pen is selected when the card loads.