| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.globals import get_session_data |
| 9 |
from complete import create_simple_input_with_history |
| 10 |
from dom import clear, svgicon, unique_id |
| 11 |
from read_book.globals import is_dark_theme |
| 12 |
from read_book.highlights import all_styles |
| 13 |
from read_book.prefs.utils import create_button_box |
| 14 |
from read_book.selection_bar import all_actions |
| 15 |
from session import session_defaults |
| 16 |
|
| 17 |
CONTAINER = unique_id('selection-settings') |
| 18 |
|
| 19 |
|
| 20 |
def set_actions(use_defaults): |
| 21 |
c = get_container() |
| 22 |
adef = all_actions() |
| 23 |
sd = get_session_data() |
| 24 |
actions = session_defaults().selection_bar_actions if use_defaults else sd.get('selection_bar_actions') |
| 25 |
current = [x for x in actions if adef[x]] |
| 26 |
c.querySelector('.current-actions').dataset.actions = JSON.stringify(current) |
| 27 |
available_actions = [x for x in adef if current.indexOf(x) is -1] |
| 28 |
c.querySelector('.available-actions').dataset.actions = JSON.stringify(available_actions) |
| 29 |
update_action_tables() |
| 30 |
quick_actions = session_defaults().selection_bar_quick_highlights if use_defaults else sd.get('selection_bar_quick_highlights') |
| 31 |
c.querySelector('.quick-actions').dataset.actions = JSON.stringify(quick_actions) |
| 32 |
update_quick_action_table() |
| 33 |
|
| 34 |
|
| 35 |
def restore_defaults(): |
| 36 |
container = get_container() |
| 37 |
sd = session_defaults() |
| 38 |
for control in container.querySelectorAll('input[name]'): |
| 39 |
val = sd[control.getAttribute('name')] |
| 40 |
if control.type is 'checkbox': |
| 41 |
control.checked = val |
| 42 |
elif control.type is 'number': |
| 43 |
control.valueAsNumber = val |
| 44 |
else: |
| 45 |
control.value = val |
| 46 |
|
| 47 |
for ta in container.querySelectorAll('textarea[name]'): |
| 48 |
val = sd[ta.getAttribute('name')] |
| 49 |
ta.value = val |
| 50 |
|
| 51 |
set_actions(True) |
| 52 |
|
| 53 |
|
| 54 |
def get_container(): |
| 55 |
return document.getElementById(CONTAINER) |
| 56 |
|
| 57 |
|
| 58 |
def transfer(name, frm, to_): |
| 59 |
c = get_container().querySelector('.' + frm) |
| 60 |
actions = JSON.parse(c.dataset.actions) |
| 61 |
idx = actions.indexOf(name) |
| 62 |
actions.splice(idx, 1) |
| 63 |
c.dataset.actions = JSON.stringify(actions) |
| 64 |
c = get_container().querySelector('.' + to_) |
| 65 |
actions = JSON.parse(c.dataset.actions) |
| 66 |
if to_ is 'current-actions': |
| 67 |
actions.unshift(name) |
| 68 |
else: |
| 69 |
actions.push(name) |
| 70 |
c.dataset.actions = JSON.stringify(actions) |
| 71 |
update_action_tables() |
| 72 |
|
| 73 |
|
| 74 |
def add_action(name): |
| 75 |
transfer(name, 'available-actions', 'current-actions') |
| 76 |
|
| 77 |
|
| 78 |
def remove_action(name): |
| 79 |
transfer(name, 'current-actions', 'available-actions') |
| 80 |
|
| 81 |
|
| 82 |
def move_action(name, up): |
| 83 |
c = get_container().querySelector('.current-actions') |
| 84 |
actions = JSON.parse(c.dataset.actions) |
| 85 |
idx = actions.indexOf(name) |
| 86 |
delta = -1 if up else 1 |
| 87 |
new_idx = (idx + delta + len(actions)) % len(actions) |
| 88 |
a, b = actions[idx], actions[new_idx] |
| 89 |
actions[new_idx], actions[idx] = a, b |
| 90 |
c.dataset.actions = JSON.stringify(actions) |
| 91 |
update_action_tables() |
| 92 |
|
| 93 |
|
| 94 |
def build_action_table(container, is_current): |
| 95 |
clear(container) |
| 96 |
adef = all_actions() |
| 97 |
table = E.table(style='margin-left: 2rem') |
| 98 |
container.appendChild(table) |
| 99 |
for action_name in JSON.parse(container.dataset.actions): |
| 100 |
ac = adef[action_name] |
| 101 |
if is_current: |
| 102 |
buttons = E.td( |
| 103 |
E.span(_('Remove'), class_='simple-link', onclick=remove_action.bind(None, action_name)), |
| 104 |
E.span('\xa0', style='min-width: 2rem; display: inline-block'), |
| 105 |
E.span(_('Up'), class_='simple-link', onclick=move_action.bind(None, action_name, True)), |
| 106 |
E.span('\xa0', style='min-width: 2rem; display: inline-block'), |
| 107 |
E.span(_('Down'), class_='simple-link', onclick=move_action.bind(None, action_name, False)), |
| 108 |
) |
| 109 |
else: |
| 110 |
buttons = E.td( |
| 111 |
E.span(_('Add'), class_='simple-link', onclick=add_action.bind(None, action_name)), |
| 112 |
) |
| 113 |
buttons.style.paddingLeft = '2rem' |
| 114 |
if ac.icon_function: |
| 115 |
icon = ac.icon_function('#add8ff') |
| 116 |
else: |
| 117 |
icon = svgicon(ac.icon) |
| 118 |
table.appendChild(E.tr(E.td(style='padding: 1ex', icon), E.td(ac.text), buttons)) |
| 119 |
|
| 120 |
|
| 121 |
def update_action_tables(): |
| 122 |
c = get_container() |
| 123 |
current = c.querySelector('.current-actions') |
| 124 |
build_action_table(current, True) |
| 125 |
current = c.querySelector('.available-actions') |
| 126 |
build_action_table(current, False) |
| 127 |
|
| 128 |
|
| 129 |
def update_quick_action_table(): |
| 130 |
c = get_container().querySelector('.quick-actions') |
| 131 |
clear(c) |
| 132 |
c.style.display = 'flex' |
| 133 |
c.style.flexWrap = 'wrap' |
| 134 |
current = {x: True for x in JSON.parse(c.dataset.actions)} |
| 135 |
actions = list(all_styles()) |
| 136 |
actions.pysort(key=def (a): return (a.friendly_name or '').toLowerCase();) |
| 137 |
for hs in actions: |
| 138 |
c.appendChild(E.label( |
| 139 |
style='margin: 1ex; display: flex; align-contents: center', |
| 140 |
hs.make_swatch(E.span(), is_dark_theme()), |
| 141 |
'\xa0', |
| 142 |
E.input(type='checkbox', value=hs.key, checked=current[hs.key]), |
| 143 |
'\xa0', |
| 144 |
hs.friendly_name, |
| 145 |
)) |
| 146 |
|
| 147 |
|
| 148 |
def selected_quick_actions(): |
| 149 |
ans = [] |
| 150 |
c = get_container().querySelector('.quick-actions') |
| 151 |
for inp in c.querySelectorAll('input:checked'): |
| 152 |
if inp.value: |
| 153 |
ans.push(inp.value) |
| 154 |
return ans |
| 155 |
|
| 156 |
|
| 157 |
def create_selection_panel(container, apply_func, cancel_func): |
| 158 |
container.appendChild(E.div(id=CONTAINER, style='margin: 1rem')) |
| 159 |
container = container.lastChild |
| 160 |
sd = get_session_data() |
| 161 |
|
| 162 |
def cb(name, text): |
| 163 |
ans = E.input(type='checkbox', name=name) |
| 164 |
if sd.get(name): |
| 165 |
ans.checked = True |
| 166 |
return E.div(style='margin-top:1ex', E.label(ans, '\xa0' + text)) |
| 167 |
|
| 168 |
def url(name, text, title): |
| 169 |
ans = create_simple_input_with_history(name, input_type='url') |
| 170 |
inp = ans.querySelector(f'input[name={name}]') |
| 171 |
inp.value = sd.get(name) or '' |
| 172 |
inp.setAttribute('size', '50') |
| 173 |
inp.style.marginTop = '1ex' |
| 174 |
return E.div(style='margin-top:1ex', E.label(text, E.br(), ans)) |
| 175 |
|
| 176 |
def cite_template_textarea(name, text, title): |
| 177 |
ans = E.textarea(name=name, style='width: 100%; margin-top: 1ex; box-sizing: border-box; flex-grow: 10') |
| 178 |
ans.value = sd.get(name) |
| 179 |
return E.div(style='margin-top:1ex', E.label(text, E.br(), ans)) |
| 180 |
|
| 181 |
container.appendChild(cb( |
| 182 |
'show_selection_bar', _('Show a popup bar with common actions next to selected text'))) |
| 183 |
container.appendChild(url( |
| 184 |
'net_search_url', _('URL to query when searching the internet'), |
| 185 |
_('The {q} in the URL is replaced by the selected text'))) |
| 186 |
|
| 187 |
container.appendChild(E.div( |
| 188 |
style='margin-top: 2ex; border-top: solid 1px; padding-top: 1ex;', |
| 189 |
_('Customize which actions are shown in the selection popup bar') |
| 190 |
)) |
| 191 |
container.appendChild(E.div(style='padding: 1ex; border-bottom: solid 1px; margin-bottom: 1ex', |
| 192 |
E.h3(_('Current actions')), |
| 193 |
E.div(class_='current-actions'), |
| 194 |
E.h3(_('Available actions')), |
| 195 |
E.div(class_='available-actions') |
| 196 |
)) |
| 197 |
container.appendChild(E.div(style='padding: 1ex; border-bottom: solid 1px; margin-bottom: 1ex', |
| 198 |
E.h3(_('Quick highlight actions')), |
| 199 |
E.div(_('Choose highlight styles that will have dedicated buttons in the selection bar to create highlights with a single click')), |
| 200 |
E.div(class_='quick-actions'), |
| 201 |
)) |
| 202 |
container.appendChild(E.h3(_('Citing text'))) |
| 203 |
container.appendChild(cite_template_textarea( |
| 204 |
'cite_text_template', _('Template for citing plain text:'))) |
| 205 |
container.appendChild(cite_template_textarea( |
| 206 |
'cite_hl_template', _('Template for citing highlighted text:'))) |
| 207 |
container.appendChild(E.div( |
| 208 |
E.div( |
| 209 |
style='margin-top: 2ex', |
| 210 |
_('{text} and {url} are available in both types of template. Additionally, {timestamp},' |
| 211 |
' {chapter}, {notes}, {style_type}, {style_kind} and {style_which} are available when citing a highlight.' |
| 212 |
).format(text='{text}', url='{url}', chapter='{chapter}', notes='{notes}', style_type='{style_type}', |
| 213 |
style_kind='{style_kind}', style_which='{style_which}', timestamp='{timestamp}', |
| 214 |
) |
| 215 |
), |
| 216 |
E.div( |
| 217 |
style='margin-top: 2ex; margin-bottom: 2ex; border-bottom: solid 1px; padding-bottom: 1ex;', |
| 218 |
_('Use "}}" and "{{" to escape "}" and "{".') |
| 219 |
) |
| 220 |
)) |
| 221 |
set_actions() |
| 222 |
|
| 223 |
container.appendChild(create_button_box(restore_defaults, apply_func, cancel_func)) |
| 224 |
|
| 225 |
|
| 226 |
develop = create_selection_panel |
| 227 |
|
| 228 |
|
| 229 |
def commit_selection(onchange): |
| 230 |
sd = get_session_data() |
| 231 |
container = get_container() |
| 232 |
changed = False |
| 233 |
save_ev = new Event('save_history') |
| 234 |
for x in container.querySelectorAll('[data-calibre-history-input]'): |
| 235 |
x.dispatchEvent(save_ev) |
| 236 |
for control in container.querySelectorAll('input[name]'): |
| 237 |
name = control.getAttribute('name') |
| 238 |
if control.type is 'checkbox': |
| 239 |
val = control.checked |
| 240 |
elif control.type is 'number': |
| 241 |
val = control.valueAsNumber |
| 242 |
else: |
| 243 |
val = control.value |
| 244 |
if val is not sd.get(name) and control.validity.valid: |
| 245 |
sd.set(name, val) |
| 246 |
changed = True |
| 247 |
actions = JSON.parse(container.querySelector('.current-actions').dataset.actions) |
| 248 |
if list(actions) != list(sd.get('selection_bar_actions')): |
| 249 |
changed = True |
| 250 |
sd.set('selection_bar_actions', actions) |
| 251 |
quick_highlights = selected_quick_actions() |
| 252 |
if list(quick_highlights) != list(sd.get('selection_bar_quick_highlights')): |
| 253 |
changed = True |
| 254 |
sd.set('selection_bar_quick_highlights', quick_highlights) |
| 255 |
# Save textarea |
| 256 |
for ta in container.querySelectorAll('textarea[name]'): |
| 257 |
name = ta.getAttribute('name') |
| 258 |
val = (ta.value or '').strip() or session_defaults()[name] |
| 259 |
old = sd.get(name) |
| 260 |
if old is not val: |
| 261 |
sd.set(name, val) |
| 262 |
changed = True |
| 263 |
if changed: |
| 264 |
onchange() |