Implement image sizes for cs comments editor as well

This commit is contained in:
Kovid Goyal 2023-10-27 19:27:39 +05:30
parent c1ca6616bd
commit e1acdb6237
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -8,6 +8,7 @@ from book_list.theme import color_scheme, get_color, get_color_as_rgba
from dom import add_extra_css, build_rule, clear, ensure_id, svgicon
from gettext import gettext as _
from iframe_comm import IframeClient, create_wrapped_iframe
from image_popup import fit_image
from modals import create_custom_dialog
from utils import html_escape
from uuid import short_uuid4
@ -75,10 +76,23 @@ def insert_image(editor):
if accepted and img.src:
align = parent.querySelector(f'input[name="{align_name}"]:checked').value
s = style_from_align(align)
markup = (
f'<img src="{img.src}" data-filename="{html_escape(img.dataset.filename)}"' +
f' data-align="{html_escape(align)}" style="{s}"></img>'
)
w = parseInt(parent.querySelector(f'input[name="width"]').value or '0')
page_width = (0 if isNaN(w) else w) or 1000000000
h = parseInt(parent.querySelector(f'input[name="height"]').value or '0')
page_height = (0 if isNaN(h) else h) or 1000000000
w, h = img.naturalWidth, img.naturalHeight
resized, nw, nh = fit_image(w, h, page_width, page_height)
if resized:
markup = (
f'<img src="{img.src}" data-filename="{html_escape(img.dataset.filename)}"' +
f' data-align="{html_escape(align)}" width="{nw}" height="{nh}" style="{s}"></img>'
)
else:
markup = (
f'<img src="{img.src}" data-filename="{html_escape(img.dataset.filename)}"' +
f' data-align="{html_escape(align)}" style="{s}"></img>'
)
editor.exec_command('insertHTML', markup)
def dialog(parent, close_modal):
@ -99,6 +113,7 @@ def insert_image(editor):
def handle_files(files):
parent.querySelector('.no-image-selected').style.display = 'none'
parent.querySelector('.image-alignment').style.display = 'none'
parent.querySelector('.image-shrink').style.display = 'none'
img = parent.getElementsByTagName('img')[0]
img.src = ''
img.dataset.filename = ''
@ -117,6 +132,7 @@ def insert_image(editor):
if f.name:
img.dataset.filename = f.name
parent.querySelector('.image-alignment').style.display = 'block'
parent.querySelector('.image-shrink').style.display = 'block'
return
alert(_('No valid image file found'))
@ -160,6 +176,17 @@ def insert_image(editor):
' ',
E.label(E.input(type='radio', name=align_name, value='right'), _('Float right')),
),
E.div(
style='display: none; margin-top: 0.5rem', class_='image-shrink',
_('Shrink image to fit (px):'),
E.div(style='display: flex; flex-wrap: wrap',
E.label(_('Width:') + '\xa0', E.input(type='number', min='0', max='10000', name='width'),
style='margin-left: 0.5rem', title=_('A value of zero means no effect')),
E.label(_('Height:') + '\xa0', E.input(type='number', min='0', max='10000', name='height'),
style='margin-left: 0.5rem', title=_('A value of zero means no effect')
)),
),
E.p(_('No image selected!'), class_='no-image-selected', style='color:red; display:none'),
),
),