| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2018, 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 |
from uuid import short_uuid4 |
| 8 |
|
| 9 |
from book_list.theme import color_scheme, get_color, get_color_as_rgba |
| 10 |
from dom import add_extra_css, build_rule, clear, ensure_id, svgicon |
| 11 |
from iframe_comm import IframeClient, create_wrapped_iframe |
| 12 |
from image_popup import fit_image |
| 13 |
from modals import create_custom_dialog |
| 14 |
from utils import html_escape |
| 15 |
from widgets import create_button |
| 16 |
|
| 17 |
CLASS_NAME = 'comments-editor' |
| 18 |
TOOLBAR_CLASS = 'comments-editor-toolbar' |
| 19 |
|
| 20 |
add_extra_css(def (): |
| 21 |
sel = '.' + TOOLBAR_CLASS + ' ' |
| 22 |
style = '' |
| 23 |
style += build_rule(sel, display='flex', flex_wrap='wrap', padding_top='0.25ex', padding_bottom='0.25ex') |
| 24 |
sel += ' > div' |
| 25 |
style += build_rule(sel, padding='0.5ex', margin_right='0.5ex', cursor='pointer') |
| 26 |
style += build_rule(sel + ':hover', color='var(--calibre-color-primary)') |
| 27 |
style += build_rule(sel + '.activated', color=get_color('window-background'), background=get_color('window-foreground')) |
| 28 |
sel += '.sep' |
| 29 |
style += build_rule(sel, border_left='solid 2px currentColor', cursor='auto') |
| 30 |
style += build_rule(sel + ':hover', color='currentColor') |
| 31 |
return style |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
def choose_block_style(proceed): |
| 36 |
create_custom_dialog(_('Choose style'), def (parent, close_modal): |
| 37 |
def action(which): |
| 38 |
close_modal() |
| 39 |
proceed(which) |
| 40 |
def s(text, tag): |
| 41 |
ans = create_button(text, action=action.bind(None, tag)) |
| 42 |
ans.style.marginBottom = '1ex' |
| 43 |
return ans |
| 44 |
spc = '\xa0' |
| 45 |
parent.appendChild(E.div( |
| 46 |
E.div(_('Choose the block style you would like to use')), |
| 47 |
E.div(class_='button-box button-box--wrap-chips', style='flex-wrap: wrap', |
| 48 |
s(_('Normal'), 'div'), spc, s('H1', 'h1'), spc, s('H2', 'h2'), spc, s('H3', 'h3'), spc, |
| 49 |
s('H4', 'h4'), spc, s('H5', 'h5'), spc, s('H6', 'h6'), spc, s(_('Quote'), 'blockquote') |
| 50 |
) |
| 51 |
)) |
| 52 |
) |
| 53 |
|
| 54 |
|
| 55 |
def insert_heading(editor_id, cmd): |
| 56 |
editor = registry[editor_id] |
| 57 |
if cmd: |
| 58 |
editor.exec_command('formatBlock', f'<{cmd}>') |
| 59 |
editor.focus() |
| 60 |
|
| 61 |
|
| 62 |
def insert_image(editor): |
| 63 |
acceptable_image_types = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'] |
| 64 |
parent_id = '' |
| 65 |
accepted = False |
| 66 |
align_name = 'align-radio-button' |
| 67 |
|
| 68 |
def on_close(evt): |
| 69 |
parent = document.getElementById(parent_id) |
| 70 |
if parent: |
| 71 |
img = parent.querySelector('img') |
| 72 |
if img: |
| 73 |
if accepted and img.src: |
| 74 |
align = parent.querySelector(f'input[name="{align_name}"]:checked').value |
| 75 |
s = style_from_align(align) |
| 76 |
|
| 77 |
w = parseInt(parent.querySelector(f'input[name="width"]').value or '0') |
| 78 |
page_width = (0 if isNaN(w) else w) or 1000000000 |
| 79 |
h = parseInt(parent.querySelector(f'input[name="height"]').value or '0') |
| 80 |
page_height = (0 if isNaN(h) else h) or 1000000000 |
| 81 |
w, h = img.naturalWidth, img.naturalHeight |
| 82 |
resized, nw, nh = fit_image(w, h, page_width, page_height) |
| 83 |
if resized: |
| 84 |
markup = ( |
| 85 |
f'<img src="{img.src}" data-filename="{html_escape(img.dataset.filename)}"' + |
| 86 |
f' data-align="{html_escape(align)}" width="{nw}" height="{nh}" style="{s}"></img>' |
| 87 |
) |
| 88 |
else: |
| 89 |
markup = ( |
| 90 |
f'<img src="{img.src}" data-filename="{html_escape(img.dataset.filename)}"' + |
| 91 |
f' data-align="{html_escape(align)}" style="{s}"></img>' |
| 92 |
) |
| 93 |
editor.exec_command('insertHTML', markup) |
| 94 |
|
| 95 |
def dialog(parent, close_modal): |
| 96 |
nonlocal parent_id |
| 97 |
parent_id = ensure_id(parent) |
| 98 |
container = parent.closest('.modal_height_container') |
| 99 |
max_container_height = container.style.maxHeight |
| 100 |
|
| 101 |
def action(ok, evt): |
| 102 |
nonlocal accepted |
| 103 |
img = parent.getElementsByTagName('img')[0] |
| 104 |
if ok and not img.src: |
| 105 |
parent.querySelector('.no-image-selected').style.display = 'block' |
| 106 |
return |
| 107 |
accepted = ok |
| 108 |
close_modal() |
| 109 |
|
| 110 |
def handle_files(files): |
| 111 |
parent.querySelector('.no-image-selected').style.display = 'none' |
| 112 |
parent.querySelector('.image-alignment').style.display = 'none' |
| 113 |
parent.querySelector('.image-shrink').style.display = 'none' |
| 114 |
img = parent.getElementsByTagName('img')[0] |
| 115 |
img.src = '' |
| 116 |
img.dataset.filename = '' |
| 117 |
img.dataset.align = '' |
| 118 |
img.parentElement.style.display = 'none' |
| 119 |
for f in files: |
| 120 |
if acceptable_image_types.indexOf(f.type) > -1: |
| 121 |
r = new FileReader() |
| 122 |
r.onload = def (): |
| 123 |
img.src = r.result |
| 124 |
r.readAsDataURL(f) |
| 125 |
h = parent.querySelector('.button-box').offsetHeight |
| 126 |
img.parentElement.style.display = 'block' |
| 127 |
non_content_height = (container.offsetHeight - parent.offsetHeight) + 'px' |
| 128 |
img.style.maxHeight = f'calc({max_container_height} - {non_content_height} - {h}px - 1rem)' |
| 129 |
if f.name: |
| 130 |
img.dataset.filename = f.name |
| 131 |
parent.querySelector('.image-alignment').style.display = 'block' |
| 132 |
parent.querySelector('.image-shrink').style.display = 'block' |
| 133 |
return |
| 134 |
alert(_('No valid image file found')) |
| 135 |
|
| 136 |
parent.appendChild(E.div( |
| 137 |
ondragenter=def (e): |
| 138 |
e.stopPropagation(), e.preventDefault() |
| 139 |
, |
| 140 |
ondragover = def (e): |
| 141 |
e.stopPropagation(), e.preventDefault() |
| 142 |
, |
| 143 |
ondrop = def (e): |
| 144 |
e.stopPropagation(), e.preventDefault() |
| 145 |
handle_files(e.dataTransfer.files) |
| 146 |
, |
| 147 |
E.div( |
| 148 |
style='display: flex;', |
| 149 |
E.div( |
| 150 |
style='display: none; min-width: 40%; max-width: 40%; margin-right: 0.5rem', |
| 151 |
E.img(style='max-width: 100%'), |
| 152 |
), |
| 153 |
E.div( |
| 154 |
E.input( |
| 155 |
type='file', accept=','.join(acceptable_image_types), |
| 156 |
onchange=def (e): handle_files(e.target.files);, style='display: none' |
| 157 |
), |
| 158 |
E.p(E.a( |
| 159 |
_('Select an image from your files'), |
| 160 |
class_='blue-link', href='javascript:void(0)', onclick=def (): |
| 161 |
parent.querySelector('input[type=file]').click() |
| 162 |
), E.span(_(' or drag and drop an image here.'))), |
| 163 |
E.div( |
| 164 |
style='display: none; margin-top: 0.5rem', class_='image-alignment', |
| 165 |
_('Place image:'), E.br(), |
| 166 |
E.label(E.input(type='radio', name=align_name, value='left'), _('Float left'), style='margin-right: 0.5rem'), |
| 167 |
' ', |
| 168 |
E.label(E.input(type='radio', name=align_name, value='inline', checked=True), _('Inline'), |
| 169 |
title=_('Place the image inline with surrounding text'), style='margin-right: 0.5rem' |
| 170 |
), |
| 171 |
' ', |
| 172 |
E.label(E.input(type='radio', name=align_name, value='right'), _('Float right')), |
| 173 |
), |
| 174 |
E.div( |
| 175 |
style='display: none; margin-top: 0.5rem', class_='image-shrink', |
| 176 |
_('Shrink image to fit (px):'), |
| 177 |
E.div(style='display: flex; flex-wrap: wrap', |
| 178 |
E.label(_('Width:') + '\xa0', E.input(type='number', min='0', max='10000', name='width'), |
| 179 |
style='margin-left: 0.5rem', title=_('A value of zero means no effect')), |
| 180 |
E.label(_('Height:') + '\xa0', E.input(type='number', min='0', max='10000', name='height'), |
| 181 |
style='margin-left: 0.5rem', title=_('A value of zero means no effect') |
| 182 |
)), |
| 183 |
), |
| 184 |
E.p(_('No image selected!'), class_='no-image-selected', style='color:red; display:none'), |
| 185 |
), |
| 186 |
), |
| 187 |
E.div(class_='button-box', |
| 188 |
create_button(_('Cancel'), action=action.bind(None, False), highlight=False), |
| 189 |
create_button(_('OK'), action=action.bind(None, True), highlight=True), |
| 190 |
), |
| 191 |
)) |
| 192 |
create_custom_dialog(_('Insert image'), dialog, on_close=on_close) |
| 193 |
|
| 194 |
|
| 195 |
def insert_link(title, msg, proceed): |
| 196 |
create_custom_dialog(title, def (parent, close_modal): |
| 197 |
def action(ok, evt): |
| 198 |
close_modal() |
| 199 |
c = evt.currentTarget.closest('.il-modal-container') |
| 200 |
url = c.querySelector('input[name="url"]').value |
| 201 |
name = c.querySelector('input[name="name"]').value |
| 202 |
proceed(ok, url, name) |
| 203 |
parent.appendChild(E.div(class_='il-modal-container', |
| 204 |
E.div(msg), |
| 205 |
E.table(style='width: 100%; max-width: 100%; margin-top: 1ex; table-layout: fixed; box-sizing: border-box', |
| 206 |
E.tr(E.td('URL:'), E.td(style='padding-bottom: 1ex', E.input(type='url', name='url', style='width: 100%'))), |
| 207 |
E.tr(E.td(_('Name (optional):')), E.td(E.input(type='text', name='name', style='width: 100%'))), |
| 208 |
), |
| 209 |
E.div(class_='button-box', |
| 210 |
create_button(_('Cancel'), action=action.bind(None, False), highlight=False), |
| 211 |
create_button(_('OK'), action=action.bind(None, True), highlight=True), |
| 212 |
), |
| 213 |
)) |
| 214 |
) |
| 215 |
|
| 216 |
|
| 217 |
def insert_link_or_image(editor_id, is_image, ok, url, title): |
| 218 |
editor = registry[editor_id] |
| 219 |
if ok: |
| 220 |
if title: |
| 221 |
markup = '<img src="{}" alt="{}"></img>' if is_image else '<a href="{}">{}</a>' |
| 222 |
editor.exec_command('insertHTML', markup.format(html_escape(url), html_escape(title))) |
| 223 |
else: |
| 224 |
cmd = 'insertImage' if is_image else 'createLink' |
| 225 |
editor.exec_command(cmd, url) |
| 226 |
editor.focus() |
| 227 |
|
| 228 |
|
| 229 |
def set_color(title, msg, proceed): |
| 230 |
create_custom_dialog(title, def (parent, close_modal): |
| 231 |
def action(ok, evt): |
| 232 |
close_modal() |
| 233 |
c = evt.currentTarget.closest('.sc-modal-container') |
| 234 |
color = c.querySelector('input[name="color"]').value |
| 235 |
proceed(ok, color) |
| 236 |
parent.appendChild(E.div(class_='sc-modal-container', |
| 237 |
E.div(msg), |
| 238 |
E.div(E.input(type='color', name='color')), |
| 239 |
E.div(class_='button-box', |
| 240 |
create_button(_('Cancel'), action=action.bind(None, False), highlight=False), |
| 241 |
create_button(_('OK'), action=action.bind(None, True), highlight=True), |
| 242 |
), |
| 243 |
)) |
| 244 |
) |
| 245 |
|
| 246 |
|
| 247 |
def change_color(editor_id, which, ok, color): |
| 248 |
editor = registry[editor_id] |
| 249 |
if ok: |
| 250 |
editor.exec_command(which, color) |
| 251 |
editor.focus() |
| 252 |
|
| 253 |
|
| 254 |
def all_editor_actions(): # {{{ |
| 255 |
if not all_editor_actions.ans: |
| 256 |
all_editor_actions.ans = { |
| 257 |
'select-all': { |
| 258 |
'icon': 'select-all', |
| 259 |
'title': _('Select all'), |
| 260 |
'execute': def (editor, activated): |
| 261 |
editor.exec_command('selectAll') |
| 262 |
}, |
| 263 |
'remove-format': { |
| 264 |
'icon': 'eraser', |
| 265 |
'title': _('Remove formatting'), |
| 266 |
'execute': def (editor, activated): |
| 267 |
editor.exec_command('removeFormat') |
| 268 |
}, |
| 269 |
'undo': { |
| 270 |
'icon': 'undo', |
| 271 |
'title': _('Undo'), |
| 272 |
'execute': def (editor, activated): |
| 273 |
editor.exec_command('undo') |
| 274 |
}, |
| 275 |
'redo': { |
| 276 |
'icon': 'redo', |
| 277 |
'title': _('Redo'), |
| 278 |
'execute': def (editor, activated): |
| 279 |
editor.exec_command('redo') |
| 280 |
}, |
| 281 |
'bold': { |
| 282 |
'icon': 'bold', |
| 283 |
'title': _('Bold'), |
| 284 |
'execute': def (editor, activated): |
| 285 |
editor.exec_command('bold') |
| 286 |
}, |
| 287 |
'italic': { |
| 288 |
'icon': 'italic', |
| 289 |
'title': _('Italic'), |
| 290 |
'execute': def (editor, activated): |
| 291 |
editor.exec_command('italic') |
| 292 |
}, |
| 293 |
'underline': { |
| 294 |
'icon': 'underline', |
| 295 |
'title': _('Underline'), |
| 296 |
'execute': def (editor, activated): |
| 297 |
editor.exec_command('underline') |
| 298 |
}, |
| 299 |
'strikethrough': { |
| 300 |
'icon': 'strikethrough', |
| 301 |
'title': _('Strikethrough'), |
| 302 |
'execute': def (editor, activated): |
| 303 |
editor.exec_command('strikeThrough') |
| 304 |
}, |
| 305 |
'superscript': { |
| 306 |
'icon': 'superscript', |
| 307 |
'title': _('Superscript'), |
| 308 |
'execute': def (editor, activated): |
| 309 |
editor.exec_command('superscript') |
| 310 |
}, |
| 311 |
'subscript': { |
| 312 |
'icon': 'subscript', |
| 313 |
'title': _('Subscript'), |
| 314 |
'execute': def (editor, activated): |
| 315 |
editor.exec_command('subscript') |
| 316 |
}, |
| 317 |
'hr': { |
| 318 |
'icon': 'hr', |
| 319 |
'title': _('Insert separator'), |
| 320 |
'execute': def (editor, activated): |
| 321 |
editor.exec_command('insertHorizontalRule') |
| 322 |
}, |
| 323 |
'format-block': { |
| 324 |
'icon': 'heading', |
| 325 |
'title': _('Style the selected text block'), |
| 326 |
'execute': def (editor, activated): |
| 327 |
choose_block_style(insert_heading.bind(None, editor.id)) |
| 328 |
}, |
| 329 |
'ul': { |
| 330 |
'icon': 'ul', |
| 331 |
'title': _('Unordered list'), |
| 332 |
'execute': def (editor, activated): |
| 333 |
editor.exec_command('insertUnorderedList') |
| 334 |
}, |
| 335 |
'ol': { |
| 336 |
'icon': 'ol', |
| 337 |
'title': _('Ordered list'), |
| 338 |
'execute': def (editor, activated): |
| 339 |
editor.exec_command('insertOrderedList') |
| 340 |
}, |
| 341 |
'indent': { |
| 342 |
'icon': 'indent', |
| 343 |
'title': _('Increase indentation'), |
| 344 |
'execute': def (editor, activated): |
| 345 |
editor.exec_command('indent') |
| 346 |
}, |
| 347 |
'outdent': { |
| 348 |
'icon': 'outdent', |
| 349 |
'title': _('Decrease indentation'), |
| 350 |
'execute': def (editor, activated): |
| 351 |
editor.exec_command('outdent') |
| 352 |
}, |
| 353 |
'justify-left': { |
| 354 |
'icon': 'justify-left', |
| 355 |
'title': _('Align left'), |
| 356 |
'execute': def (editor, activated): |
| 357 |
editor.exec_command('justifyLeft') |
| 358 |
}, |
| 359 |
'justify-full': { |
| 360 |
'icon': 'justify-full', |
| 361 |
'title': _('Align justified'), |
| 362 |
'execute': def (editor, activated): |
| 363 |
editor.exec_command('justifyFull') |
| 364 |
}, |
| 365 |
'justify-center': { |
| 366 |
'icon': 'justify-center', |
| 367 |
'title': _('Align center'), |
| 368 |
'execute': def (editor, activated): |
| 369 |
editor.exec_command('justifyCenter') |
| 370 |
}, |
| 371 |
'justify-right': { |
| 372 |
'icon': 'justify-right', |
| 373 |
'title': _('Align right'), |
| 374 |
'execute': def (editor, activated): |
| 375 |
editor.exec_command('justifyRight') |
| 376 |
}, |
| 377 |
'insert-link': { |
| 378 |
'icon': 'insert-link', |
| 379 |
'title': _('Insert a link or linked image'), |
| 380 |
'execute': def (editor, activated): |
| 381 |
insert_link(_('Insert a link'), _('Enter the link URL and optionally the link name'), insert_link_or_image.bind(None, editor.id, False)) |
| 382 |
}, |
| 383 |
'insert-image': { |
| 384 |
'icon': 'image', |
| 385 |
'title': _('Insert an image'), |
| 386 |
'execute': def (editor, activated): |
| 387 |
if editor.insert_image_files: |
| 388 |
insert_image(editor) |
| 389 |
else: |
| 390 |
insert_link( |
| 391 |
_('Insert an image'), |
| 392 |
_('Enter the image URL and optionally the image name'), |
| 393 |
insert_link_or_image.bind(None, editor.id, True) |
| 394 |
) |
| 395 |
}, |
| 396 |
'fg': { |
| 397 |
'icon': 'fg', |
| 398 |
'title': _('Change the text color'), |
| 399 |
'execute': def (editor, activated): |
| 400 |
set_color(_('Choose text color'), _('Choose the color below'), change_color.bind(None, editor.id, 'foreColor')) |
| 401 |
}, |
| 402 |
'bg': { |
| 403 |
'icon': 'bg', |
| 404 |
'title': _('Change the highlight color'), |
| 405 |
'execute': def (editor, activated): |
| 406 |
set_color(_('Choose highlight color'), _('Choose the color below'), change_color.bind(None, editor.id, 'hiliteColor')) |
| 407 |
}, |
| 408 |
} |
| 409 |
return all_editor_actions.ans |
| 410 |
# }}} |
| 411 |
|
| 412 |
|
| 413 |
def style_from_align(align): |
| 414 |
if align is 'left': |
| 415 |
return 'float: left; margin-right: 0.5em' |
| 416 |
if align is 'right': |
| 417 |
return 'float: right; margin-left: 0.5em' |
| 418 |
return '' |
| 419 |
|
| 420 |
|
| 421 |
class CommentsEditorBoss: |
| 422 |
def __init__(self): |
| 423 |
handlers = { |
| 424 |
'initialize': self.initialize, |
| 425 |
'set_html': self.set_html, |
| 426 |
'get_html': self.get_html, |
| 427 |
'exec_command': self.exec_command, |
| 428 |
'focus': self.focus, |
| 429 |
} |
| 430 |
self.comm = IframeClient(handlers, 'comments-editor-iframe') |
| 431 |
|
| 432 |
def initialize(self, data): |
| 433 |
window.onerror = self.onerror |
| 434 |
clear(document.body) |
| 435 |
document.execCommand('defaultParagraphSeparator', False, 'div') |
| 436 |
document.execCommand('styleWithCSS', False, False) |
| 437 |
document.body.style.margin = '0' |
| 438 |
document.body.style.padding = '0' |
| 439 |
document.body.style.fontFamily = window.default_font_family |
| 440 |
document.body.appendChild(E.style('div:focus { outline: none }')) |
| 441 |
document.body.appendChild(E.div(style='width: 100%; padding: 0.5rem; margin: 0; border-width: 0; box-sizing: border-box')) |
| 442 |
document.body.lastChild.contentEditable = True |
| 443 |
document.body.lastChild.addEventListener('keyup', self.update_state) |
| 444 |
document.body.lastChild.addEventListener('mouseup', self.update_state) |
| 445 |
document.body.lastChild.focus() |
| 446 |
self.update_state() |
| 447 |
|
| 448 |
def focus(self): |
| 449 |
document.body.lastChild.focus() |
| 450 |
|
| 451 |
def onerror(self, msg, script_url, line_number, column_number, error_object): |
| 452 |
if error_object is None: |
| 453 |
# This happens for cross-domain errors (probably javascript injected |
| 454 |
# into the browser via extensions/ userscripts and the like). It also |
| 455 |
# happens all the time when using Chrome on Safari |
| 456 |
console.log(f'Unhandled error from external javascript, ignoring: {msg} {script_url} {line_number}') |
| 457 |
else: |
| 458 |
console.log(error_object) |
| 459 |
|
| 460 |
def set_html(self, data): |
| 461 |
if data.color_scheme: |
| 462 |
document.documentElement.style.colorScheme = data.color_scheme |
| 463 |
document.body.style.color = data.color |
| 464 |
document.body.lastChild.innerHTML = data.html |
| 465 |
|
| 466 |
def get_html(self, data): |
| 467 |
c = document.body.lastChild.cloneNode(True) |
| 468 |
images = v'{}' |
| 469 |
if data.extract_images: |
| 470 |
for img in c.getElementsByTagName('img'): |
| 471 |
if img.src: |
| 472 |
key = short_uuid4() |
| 473 |
images[key] = {'data': img.src, 'filename': img.dataset.filename} |
| 474 |
if img.dataset.filename: # this is an added image |
| 475 |
s = style_from_align(img.dataset.align) |
| 476 |
if s: |
| 477 |
img.setAttribute('style', s) |
| 478 |
else: |
| 479 |
img.removeAttribute('style') |
| 480 |
img.src = key |
| 481 |
v'delete img.dataset.filename' |
| 482 |
v'delete img.dataset.align' |
| 483 |
self.comm.send_message('html', html=c.innerHTML, extra_data={'images': images, 'text': c.innerText}) |
| 484 |
|
| 485 |
def exec_command(self, data): |
| 486 |
document.execCommand(data.name, False, data.value) |
| 487 |
self.update_state() |
| 488 |
|
| 489 |
def update_state(self): |
| 490 |
state = {name: document.queryCommandState(name) for name in 'bold italic underline superscript subscript'.split(' ')} |
| 491 |
state.strikethrough = document.queryCommandState('strikeThrough') |
| 492 |
self.comm.send_message('update_state', state=state) |
| 493 |
|
| 494 |
|
| 495 |
registry = {} |
| 496 |
|
| 497 |
|
| 498 |
def destroy_removed_editors(): |
| 499 |
for k in Object.keys(registry): |
| 500 |
if not document.getElementById(k): |
| 501 |
registry[k].destroy() |
| 502 |
v'delete registry[k]' |
| 503 |
|
| 504 |
|
| 505 |
def add_editor(editor): |
| 506 |
destroy_removed_editors() |
| 507 |
registry[editor.id] = editor |
| 508 |
|
| 509 |
|
| 510 |
def destroy_editor(container): |
| 511 |
v'delete registry[container.querySelector("iframe").id]' |
| 512 |
|
| 513 |
|
| 514 |
class Editor: |
| 515 |
def __init__(self, iframe_kw, insert_image_files): |
| 516 |
handlers = { |
| 517 |
'ready': self.on_iframe_ready, |
| 518 |
'html': self.on_html_received, |
| 519 |
'update_state': self.update_state, |
| 520 |
} |
| 521 |
iframe, self.iframe_wrapper = create_wrapped_iframe( |
| 522 |
handlers, _('Loading comments editor...'), 'book_list.comments_editor', iframe_kw) |
| 523 |
self.id = iframe.id |
| 524 |
self.ready = False |
| 525 |
self.pending_set_html = None |
| 526 |
self.pending_get_html = [] |
| 527 |
self.get_html_callbacks = [] |
| 528 |
self.iframe_obj = iframe |
| 529 |
self.insert_image_files = v'!!insert_image_files' |
| 530 |
|
| 531 |
def init(self): |
| 532 |
self.iframe_wrapper.init() |
| 533 |
|
| 534 |
def destroy(self): |
| 535 |
self.iframe_wrapper.destroy() |
| 536 |
self.get_html_callbacks = [] |
| 537 |
|
| 538 |
def focus(self): |
| 539 |
self.iframe.contentWindow.focus() |
| 540 |
if self.ready: |
| 541 |
self.iframe_wrapper.send_message('focus') |
| 542 |
|
| 543 |
@property |
| 544 |
def iframe(self): |
| 545 |
return self.iframe_wrapper.iframe |
| 546 |
|
| 547 |
def on_iframe_ready(self, msg): |
| 548 |
self.ready = True |
| 549 |
return self.after_iframe_initialized |
| 550 |
|
| 551 |
def after_iframe_initialized(self): |
| 552 |
if self.pending_set_html is not None: |
| 553 |
self.set_html(self.pending_set_html) |
| 554 |
self.pending_set_html = None |
| 555 |
if self.pending_get_html.length > 0: |
| 556 |
for x in self.pending_get_html: |
| 557 |
self.get_html(x) |
| 558 |
|
| 559 |
def set_html(self, html): |
| 560 |
if not self.ready: |
| 561 |
self.pending_set_html = html |
| 562 |
return |
| 563 |
rgba = get_color_as_rgba('window-foreground') |
| 564 |
self.iframe_wrapper.send_message('set_html', html=html, color_scheme=color_scheme(), color=f'rgba({rgba[0]},{rgba[1]},{rgba[2]},{rgba[3]})') |
| 565 |
|
| 566 |
def get_html(self, proceed): |
| 567 |
self.get_html_callbacks.push(proceed) |
| 568 |
if self.ready: |
| 569 |
self.iframe_wrapper.send_message('get_html', extract_images=self.insert_image_files) |
| 570 |
else: |
| 571 |
self.pending_get_html.push(proceed) |
| 572 |
|
| 573 |
def on_html_received(self, data): |
| 574 |
if self.get_html_callbacks.length: |
| 575 |
for f in self.get_html_callbacks: |
| 576 |
f(data.html, data.extra_data) |
| 577 |
self.get_html_callbacks = [] |
| 578 |
|
| 579 |
def exec_command(self, name, value=None): |
| 580 |
if self.ready: |
| 581 |
self.iframe_wrapper.send_message('exec_command', name=name, value=value) |
| 582 |
|
| 583 |
def update_state(self, data): |
| 584 |
c = self.iframe.closest('.' + CLASS_NAME) |
| 585 |
for name in Object.keys(data.state): |
| 586 |
div = c.querySelector(f'.{TOOLBAR_CLASS} > [name="{name}"]') |
| 587 |
if div: |
| 588 |
if data.state[name]: |
| 589 |
div.classList.add('activated') |
| 590 |
else: |
| 591 |
div.classList.remove('activated') |
| 592 |
|
| 593 |
|
| 594 |
def create_editor(insert_image_files): |
| 595 |
iframe_kw = { |
| 596 |
'sandbox': 'allow-scripts', 'seamless': True, 'style': 'flex-grow: 10; border: solid 1px currentColor' |
| 597 |
} |
| 598 |
editor = Editor(iframe_kw, insert_image_files) |
| 599 |
iframe = editor.iframe_obj |
| 600 |
v'delete editor.iframe_obj' |
| 601 |
add_editor(editor) |
| 602 |
return iframe, editor |
| 603 |
|
| 604 |
|
| 605 |
def action_activated(editor_id, ac_name, evt): |
| 606 |
editor = registry[editor_id] |
| 607 |
if not editor: |
| 608 |
return |
| 609 |
action = all_editor_actions()[ac_name] |
| 610 |
if not action: |
| 611 |
return |
| 612 |
button = evt.currentTarget |
| 613 |
action.execute(editor, button.classList.contains('activated')) |
| 614 |
editor.focus() |
| 615 |
|
| 616 |
|
| 617 |
def add_action(toolbar, ac_name, action, editor_id): |
| 618 |
b = E.div(svgicon(action.icon), title=action.title, onclick=action_activated.bind(None, editor_id, ac_name), name=ac_name) |
| 619 |
toolbar.appendChild(b) |
| 620 |
|
| 621 |
|
| 622 |
def create_comments_editor(container, options): |
| 623 |
options = options or {} |
| 624 |
iframe, editor = create_editor(options.insert_image_files) |
| 625 |
toolbars = E.div(style='flex-grow: 0') |
| 626 |
toolbar1 = E.div(class_=TOOLBAR_CLASS) |
| 627 |
toolbar2 = E.div(class_=TOOLBAR_CLASS) |
| 628 |
toolbar3 = E.div(class_=TOOLBAR_CLASS) |
| 629 |
toolbars.appendChild(toolbar1), toolbars.appendChild(toolbar2), toolbars.appendChild(toolbar3) |
| 630 |
acmap = all_editor_actions() |
| 631 |
|
| 632 |
def add(toolbar, ac_name): |
| 633 |
if ac_name: |
| 634 |
if acmap[ac_name]: |
| 635 |
add_action(toolbar, ac_name, acmap[ac_name], editor.id) |
| 636 |
else: |
| 637 |
toolbar.appendChild(E.div(class_='sep')) |
| 638 |
|
| 639 |
for ac_name in 'undo redo select-all remove-format bold italic underline strikethrough'.split(' '): |
| 640 |
add(toolbar1, ac_name) |
| 641 |
|
| 642 |
for ac_name in 'hr superscript subscript format-block ul ol indent outdent'.split(' '): |
| 643 |
add(toolbar2, ac_name) |
| 644 |
|
| 645 |
for ac_name in 'justify-left justify-center justify-right justify-full insert-link insert-image fg bg'.split(' '): |
| 646 |
add(toolbar3, ac_name) |
| 647 |
|
| 648 |
container.setAttribute('style', (container.getAttribute('style') or '') + ';display: flex; flex-direction: column; align-items: stretch') |
| 649 |
container.appendChild(toolbars) |
| 650 |
container.appendChild(iframe) |
| 651 |
container.classList.add(CLASS_NAME) |
| 652 |
return editor |
| 653 |
|
| 654 |
|
| 655 |
def focus_comments_editor(container): |
| 656 |
iframe = container.querySelector('iframe') |
| 657 |
editor = registry[iframe.getAttribute('id')] |
| 658 |
if editor: |
| 659 |
editor.focus() |
| 660 |
|
| 661 |
|
| 662 |
def set_comments_html(container, html): |
| 663 |
iframe = container.querySelector('iframe') |
| 664 |
eid = iframe.getAttribute('id') |
| 665 |
editor = registry[eid] |
| 666 |
editor.set_html(html or '') |
| 667 |
|
| 668 |
|
| 669 |
def get_comments_html(container, proceed): |
| 670 |
iframe = container.querySelector('iframe') |
| 671 |
eid = iframe.getAttribute('id') |
| 672 |
editor = registry[eid] |
| 673 |
editor.get_html(proceed) |
| 674 |
|
| 675 |
|
| 676 |
def develop(container): |
| 677 |
container.setAttribute('style', 'width: 100%; min-height: 90vh; display: flex; flex-direction: column; align-items: stretch') |
| 678 |
editor = create_comments_editor(container) |
| 679 |
set_comments_html(container, '<p>Testing, <i>testing</i> 123...') |
| 680 |
focus_comments_editor(container) |
| 681 |
editor.init() |
| 682 |
|
| 683 |
|
| 684 |
@no_prune |
| 685 |
def main(): |
| 686 |
main.boss = CommentsEditorBoss() |