Debian Codegraph @

src/pyj/read_book/prefs/user_stylesheet.pyj

raw
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 encodings import hexlify
7 from gettext import gettext as _
8
9 from ajax import absolute_path
10 from book_list.globals import get_session_data
11 from dom import ensure_id, unique_id
12 from read_book.globals import runtime, ui_operations
13 from read_book.prefs.utils import create_button_box
14 from session import session_defaults
15 from viewer.constants import FAKE_HOST, FAKE_PROTOCOL
16 from widgets import create_button, create_select
17
18 BLANK = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
19
20
21 def change_background_image(img_id):
22 ui_operations.change_background_image(img_id)
23
24
25 def clear_image(img_id):
26 i = document.getElementById(img_id)
27 i.src = BLANK
28 i.dataset.url = ''
29
30
31 def modify_background_image_url_for_fetch(url):
32 if not url:
33 return BLANK
34 if runtime.is_standalone_viewer:
35 if url.startswith(f'{FAKE_PROTOCOL}:'):
36 return url
37 encoded = hexlify(url)
38 return f'{FAKE_PROTOCOL}://{FAKE_HOST}/reader-background-{encoded}'
39 if url.startswith(f'{FAKE_PROTOCOL}:'):
40 x = str.split(url, '/')[-1].partition('?')[0].partition('-')[2]
41 return absolute_path(f'reader-background/{x}')
42 return url
43
44
45 def standalone_background_widget(sd):
46 url = sd.get('background_image')
47 src = modify_background_image_url_for_fetch(url)
48 img_id = unique_id('bg-image')
49
50 return E.div(
51 style='display: flex; align-items: center',
52 E.div(E.img(src=src, data_url=url, id=img_id, class_='bg-image-preview', style='width: 75px; height: 75px; border: solid 1px')),
53 E.div('\xa0', style='margin: 0.5rem'),
54 create_button(_('Change image'), action=change_background_image.bind(None, img_id)),
55 E.div('\xa0', style='margin: 0.5rem'),
56 create_button(_('Clear image'), action=clear_image.bind(None, img_id)),
57 )
58
59
60 def background_widget(sd):
61 if runtime.is_standalone_viewer:
62 return standalone_background_widget(sd)
63 return E.div(style='margin-bottom: 1ex', E.label(
64 _('Image URL') + ':\xa0',
65 E.input(type='url', name='background_image', value=sd.get('background_image') or '')
66 ))
67
68
69 def background_style_widget(sd):
70 title = _('Background image style')
71 c, s = create_select(title, sd.get('background_image_style'), scaled=_('Scaled'), tiled=_('Tiled'))
72 s.setAttribute('name', 'background_image_style')
73 return E.div(style='margin-bottom: 1ex', E.label(title + ':\xa0', c))
74
75
76 def background_fade_widget(sd):
77 return E.div(E.label(
78 _('Background image fade (%)') + ':\xa0',
79 E.input(
80 name='background_image_fade', type='number', max='100', min='0', step='1',
81 value='' + sd.get('background_image_fade'), style='min-width: 8ch',
82 title=_('Fading of the background image is done by blending it with the background color')
83 ),
84 ))
85
86
87 def restore_defaults():
88 container = document.getElementById(create_user_stylesheet_panel.container_id)
89 container.querySelector('[name=user-stylesheet]').value = ''
90 if runtime.is_standalone_viewer:
91 i = container.querySelector('img')
92 clear_image(i.id)
93 else:
94 container.querySelector('[name=background_image]').value = ''
95 container.querySelector('select[name=background_image_style]').value = session_defaults().background_image_style
96 container.querySelector('input[name=background_image_fade]').value = str(session_defaults().background_image_fade)
97
98
99 def create_user_stylesheet_panel(container, apply_func, cancel_func):
100 sd = get_session_data()
101 create_user_stylesheet_panel.container_id = ensure_id(container)
102 container.appendChild(
103 E.div(
104 style='min-height: 75vh; display: flex; flex-flow: column; margin: 1ex 1rem; padding: 1ex 0',
105 E.div(
106 style='border-bottom: solid 1px; margin-bottom: 1.5ex; padding-bottom: 1.5ex',
107 E.div(_('Choose a background image to display behind the book text'), style='margin-bottom: 1.5ex'),
108 background_widget(sd),
109 background_style_widget(sd),
110 background_fade_widget(sd),
111 ),
112 E.div(
113 style='flex-grow: 10; display: flex; flex-flow: column',
114 E.div(
115 _('A CSS style sheet that can be used to control the look and feel of the text. For examples, click'), ' ',
116 E.a(class_='blue-link', title=_('Examples of user style sheets'),
117 target=('_self' if runtime.is_standalone_viewer else '_blank'),
118 href='https://www.mobileread.com/forums/showthread.php?t=51500', _('here.')),
119 ' ', _(
120 'Note that you can use the selectors body.calibre-viewer-paginated and body.calibre-viewer-scrolling'
121 ' to target the Paged and Flow modes. Similarly, use body.calibre-viewer-light-colors and'
122 ' body.calibre-viewer-dark-colors to target light and dark color schemes.')
123 ),
124 E.textarea(name='user-stylesheet', style='width: 100%; margin-top: 1ex; box-sizing: border-box; flex-grow: 10'),
125 )
126 )
127 )
128 val = sd.get('user_stylesheet')
129 if val:
130 container.querySelector('[name=user-stylesheet]').value = val
131 container.appendChild(E.div(style='margin: 1rem', create_button_box(restore_defaults, apply_func, cancel_func)))
132
133
134 develop = create_user_stylesheet_panel
135
136
137 def commit_user_stylesheet(onchange, container):
138 sd = get_session_data()
139 ta = container.querySelector('[name=user-stylesheet]')
140 val = ta.value or ''
141 old = sd.get('user_stylesheet')
142 changed = False
143 if old is not val:
144 sd.set('user_stylesheet', val)
145 changed = True
146 if runtime.is_standalone_viewer:
147 bg_image = container.querySelector('img.bg-image-preview').dataset.url
148 if bg_image is BLANK or not bg_image:
149 bg_image = None
150 else:
151 bg_image = container.querySelector('input[name=background_image]').value
152 old = sd.get('background_image')
153 if old is not bg_image:
154 sd.set('background_image', bg_image)
155 changed = True
156 old = sd.get('background_image_style')
157 bis = container.querySelector('select[name=background_image_style]').value
158 if bis is not old:
159 changed = True
160 sd.set('background_image_style', bis)
161 old = int(sd.get('background_image_fade'))
162 bif = int(container.querySelector('input[name=background_image_fade]').value)
163 if old is not bif:
164 changed = True
165 sd.set('background_image_fade', bif)
166 if changed:
167 onchange()