Content server: When defining a color scheme for the in browser viewer allow specifying the link color as well as the foreground and background. Fixes #1735904 [Feature request: link color in schemes](https://bugs.launchpad.net/calibre/+bug/1735904)

This commit is contained in:
Kovid Goyal 2018-08-06 17:05:17 +05:30
parent ef14d73112
commit d18cf87693
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 39 additions and 13 deletions

View File

@ -49,9 +49,9 @@ def set_toc_anchor_map(val):
default_color_schemes = { default_color_schemes = {
'white':{'foreground':'#000000', 'background':'#ffffff', 'name':_('White')}, 'white':{'foreground':'#000000', 'background':'#ffffff', 'name':_('White')},
'black':{'foreground':'#ffffff', 'background':'#000000', 'name':_('Black')}, 'black':{'foreground':'#ffffff', 'background':'#000000', 'link': '#4f81bd', 'name':_('Black')},
'sepia-light':{'foreground':'#39322B', 'background':'#F6F3E9', 'name':_('Sepia light')}, 'sepia-light':{'foreground':'#39322B', 'background':'#F6F3E9', 'name':_('Sepia light')},
'sepia-dark': {'background':'#39322B', 'foreground':'#F6F3E9', 'name':_('Sepia dark')}, 'sepia-dark': {'background':'#39322B', 'foreground':'#F6F3E9', 'link': '#4f81bd', 'name':_('Sepia dark')},
} }
register_callback(def(): register_callback(def():

View File

@ -58,8 +58,10 @@ def new_color_scheme(ev):
container = get_container() container = get_container()
container.lastChild.style.display = 'block' container.lastChild.style.display = 'block'
for inp in container.lastChild.querySelectorAll('input'): for inp in container.lastChild.querySelectorAll('input'):
n = inp.getAttribute('name') if inp.name is 'link_color_type':
inp.value = {'name':'', 'bg':'#ffffff', 'fg':'#000000'}[n] inp.checked = inp.value is 'default'
else:
inp.value = {'name':'', 'bg':'#ffffff', 'fg':'#000000', 'link': '#0000ee'}[inp.name]
container.lastChild.querySelector('input').focus() container.lastChild.querySelector('input').focus()
return container return container
@ -68,10 +70,16 @@ def edit_color_scheme(ev):
ccs = current_color_scheme(container) ccs = current_color_scheme(container)
all_schemes = all_color_schemes() all_schemes = all_color_schemes()
if all_schemes[ccs]: if all_schemes[ccs]:
scheme = all_schemes[ccs]
container = document.getElementById(EDIT_SCHEME) container = document.getElementById(EDIT_SCHEME)
container.querySelector('input').value = all_schemes[ccs].name container.querySelector('input').value = scheme.name
container.querySelector('input[name=bg]').value = all_schemes[ccs].background container.querySelector('input[name=bg]').value = scheme.background
container.querySelector('input[name=fg]').value = all_schemes[ccs].foreground container.querySelector('input[name=fg]').value = scheme.foreground
if scheme.link:
container.querySelector('input[value=custom]').checked = True
container.querySelector('input[name=link]').value = scheme.link
else:
container.querySelector('input[value=default]').checked = True
def remove_color_scheme(ev): def remove_color_scheme(ev):
ccs = current_color_scheme() ccs = current_color_scheme()
@ -110,17 +118,19 @@ def add_color_scheme(ev):
error_dialog(_('Name not specified'), _( error_dialog(_('Name not specified'), _(
'You must specify a name for the color scheme')) 'You must specify a name for the color scheme'))
return return
bg = div.querySelector('input[name=bg]').value colors = {}
fg = div.querySelector('input[name=fg]').value for col in ('bg', 'fg', 'link'):
for col in (bg, fg): colors[col] = div.querySelector(f'input[name={col}]').value
if not /^#[0-9A-F]{6}$/i.test(bg): if not /^#[0-9A-F]{6}$/i.test(colors[col]):
error_dialog(_('Invalid color'), _( error_dialog(_('Invalid color'), _(
'The color {} is not a valid color').format(col)) 'The color {} is not a valid color').format(colors[col]))
return return
key = '*' + name key = '*' + name
sd = get_session_data() sd = get_session_data()
ucs = sd.get('user_color_schemes') ucs = sd.get('user_color_schemes')
ucs[key] = {'name':name, 'foreground':fg, 'background':bg} ucs[key] = {'name':name, 'foreground':colors.fg, 'background':colors.bg}
if div.querySelector('input[name=link_color_type]:checked').value is 'custom':
ucs[key]['link'] = colors.link
sd.set('user_color_schemes', ucs) sd.set('user_color_schemes', ucs)
create_color_buttons() create_color_buttons()
set_current_color_scheme(key) set_current_color_scheme(key)
@ -186,6 +196,13 @@ def create_colors_panel(container):
E.tr(E.td(_('Name:')), E.td(E.input(name='name'))), E.tr(E.td(_('Name:')), E.td(E.input(name='name'))),
E.tr(E.td(_('Background:')), E.td(E.input(name='bg', type='color', value='#ffffff'))), E.tr(E.td(_('Background:')), E.td(E.input(name='bg', type='color', value='#ffffff'))),
E.tr(E.td(_('Foreground:')), E.td(E.input(name='fg', type='color', value='#000000'))), E.tr(E.td(_('Foreground:')), E.td(E.input(name='fg', type='color', value='#000000'))),
E.tr(E.td(_('Link:')), E.td(
E.label(E.input(type='radio', name='link_color_type', value='default'), _('Default')),
'\xa0\xa0',
E.label(E.input(type='radio', name='link_color_type', value='custom'), _('Custom')),
'\xa0',
E.input(name='link', type='color', value='#000000')
)),
), ),
E.div(style="display:flex; justify-content: flex-end; margin: 1ex 1em", E.div(style="display:flex; justify-content: flex-end; margin: 1ex 1em",
create_button(_('OK'), 'check', add_color_scheme), E.span('\xa0'), create_button(_('Cancel'), 'close', add_color_scheme.bind('cancel')) create_button(_('OK'), 'check', add_color_scheme), E.span('\xa0'), create_button(_('Cancel'), 'close', add_color_scheme.bind('cancel'))

View File

@ -26,6 +26,15 @@ def apply_colors():
for elem in (document.documentElement, document.body): for elem in (document.documentElement, document.body):
elem.style.color = opts.color_scheme.foreground elem.style.color = opts.color_scheme.foreground
elem.style.backgroundColor = opts.color_scheme.background elem.style.backgroundColor = opts.color_scheme.background
ss = document.getElementById('calibre-color-scheme-style-overrides')
if not ss:
ss = E.style(id='calibre-color-scheme-style-overrides', type='text/css')
document.documentElement.appendChild(ss)
if opts.color_scheme.link:
c = opts.color_scheme.link
ss.textContent = f':link, :link * {{ color: {c} !important }} :visited, :visited * {{ color: {c} !important }}'
else:
ss.textContent = ''
def apply_stylesheet(): def apply_stylesheet():