diff --git a/README.md b/README.md index 427352c..3c11552 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ ![Demo](demo.gif) Adds a font color picker to the Etherpad toolbar, with HTML export support. +If authorship colors are enabled, the chosen font color is still saved but +the editor will warn that author colors are masking the preview. ## Install @@ -26,4 +28,4 @@ To reposition the color picker in the toolbar, add `fontColor` to your `settings ## License -Apache-2.0 \ No newline at end of file +Apache-2.0 diff --git a/static/js/index.js b/static/js/index.js index dabf32a..8052707 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -3,15 +3,46 @@ const {inlineAttribute} = require('ep_plugin_helpers/attributes'); const colors = ['black', 'red', 'green', 'blue', 'yellow', 'orange']; +const authorColorsMessageTitle = 'Font colors are hidden by authorship colors'; +const authorColorsMessageText = + 'Your font color was saved. Turn off "Colors" in Settings to preview it.'; +let authorColorsNoticeShown = false; const fontColor = inlineAttribute({attr: 'color', values: colors}); exports.aceAttribsToClasses = fontColor.aceAttribsToClasses; exports.aceCreateDomLine = fontColor.aceCreateDomLine; +const hasAuthorColorsEnabled = () => $('iframe[name="ace_outer"]').contents() + .find('iframe[name="ace_inner"]').contents().find('#innerdocbody').hasClass('authorColors'); + +const getInnerDocBody = () => $('iframe[name="ace_outer"]').contents() + .find('iframe[name="ace_inner"]').contents().find('#innerdocbody').get(0); + +const updateToolbarHint = () => { + const authorColorsEnabled = hasAuthorColorsEnabled(); + const hint = authorColorsEnabled ? authorColorsMessageText : ''; + $('.font-color-icon a, .color-selection, #color-selection').attr('title', hint); + if (!authorColorsEnabled) authorColorsNoticeShown = false; +}; + +const maybeShowAuthorColorsNotice = () => { + if (!hasAuthorColorsEnabled() || authorColorsNoticeShown || !$.gritter) return; + authorColorsNoticeShown = true; + $.gritter.add({ + title: authorColorsMessageTitle, + text: authorColorsMessageText, + }); +}; + // Bind the event handler to the toolbar buttons exports.postAceInit = (hook, context) => { const hs = $('.color-selection, #color-selection'); + const scheduleHintRefresh = () => { + [0, 50, 150, 400, 800].forEach((delay) => setTimeout(() => { + updateToolbarHint(); + }, delay)); + }; hs.on('change', function () { const value = $(this).val(); const intValue = parseInt(value, 10); @@ -21,6 +52,7 @@ exports.postAceInit = (hook, context) => { }, 'insertColor', true); hs.val('dummy'); context.ace.focus(); + maybeShowAuthorColorsNotice(); } }); $('.font_color').hover(() => { @@ -31,6 +63,15 @@ exports.postAceInit = (hook, context) => { $('#font-color').toggle(); context.ace.focus(); }); + const innerDocBody = getInnerDocBody(); + if (innerDocBody && typeof MutationObserver !== 'undefined') { + new MutationObserver((mutations) => { + if (mutations.some((mutation) => mutation.attributeName === 'class')) updateToolbarHint(); + }).observe(innerDocBody, {attributes: true, attributeFilter: ['class']}); + } + $('#options-colorscheck, #padsettings-options-colorscheck, ' + + 'label[for="options-colorscheck"], label[for="padsettings-options-colorscheck"]') + .on('click change', scheduleHintRefresh); // Re-render the niceSelect dropdown whenever the active UI language // changes. html10n rewrites the underlying