Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed some read-only mode buttons bugs #832

Merged
merged 7 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dist/suneditor.min.js

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions sample/html/out/document-util.html
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,40 @@ <h5>Parameters:</h5>
<dl class="details"></dl>


<h4 class="name" id="arrayIncludes"><span class="type-signature"></span>arrayIncludes<span
class="signature">(array, element)</span><span class="type-signature"> &rarr; {Boolean}</span></h4>
<div class="description">
Check if an array contains an element
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>array</code></td>
<td class="type">
<span class="param-type">Array</span>
</td>
<td class="description last">element array</td>
</tr>
<tr>
<td class="name"><code>element</code></td>
<td class="type">
<span class="param-type">Element</span>
</td>
<td class="description last">Element to check for</td>
</tr>
</tbody>
</table>
<dl class="details"></dl>


<h4 class="name" id="getArrayIndex"><span class="type-signature"></span>getArrayIndex<span
class="signature">(array, element)</span><span class="type-signature"> &rarr; {Number}</span></h4>
<div class="description">
Expand Down Expand Up @@ -2542,6 +2576,7 @@ <h3>util</h3>
<li><a href="document-util.html#copyTagAttributes">copyTagAttributes</a></li>
<li><a href="document-util.html#copyFormatAttributes">copyFormatAttributes</a></li>
<li><a href="document-util.html#getArrayItem">getArrayItem</a></li>
<li><a href="document-util.html#arrayIncludes">arrayIncludes</a></li>
<li><a href="document-util.html#getArrayIndex">getArrayIndex</a></li>
<li><a href="document-util.html#nextIdx">nextIdx</a></li>
<li><a href="document-util.html#prevIdx">prevIdx</a></li>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,8 @@ export default {
fullScreen: ['se-code-view-enabled se-resizing-enabled _se_command_fullScreen', lang.toolbar.fullScreen, 'fullScreen', '', icons.expansion],
showBlocks: ['_se_command_showBlocks', lang.toolbar.showBlocks, 'showBlocks', '', icons.show_blocks],
codeView: ['se-code-view-enabled se-resizing-enabled _se_command_codeView', lang.toolbar.codeView, 'codeView', '', icons.code_view],
undo: ['_se_command_undo se-resizing-enabled', lang.toolbar.undo + '<span class="se-shortcut">' + (shortcutsDisable.indexOf('undo') > -1 ? '' : cmd + '+<span class="se-shortcut-key">Z</span>') + '</span>', 'undo', '', icons.undo],
redo: ['_se_command_redo se-resizing-enabled', lang.toolbar.redo + '<span class="se-shortcut">' + (shortcutsDisable.indexOf('undo') > -1 ? '' : cmd + '+<span class="se-shortcut-key">Y</span> / ' + cmd + addShift + '+<span class="se-shortcut-key">Z</span>') + '</span>', 'redo', '', icons.redo],
undo: ['_se_command_undo', lang.toolbar.undo + '<span class="se-shortcut">' + (shortcutsDisable.indexOf('undo') > -1 ? '' : cmd + '+<span class="se-shortcut-key">Z</span>') + '</span>', 'undo', '', icons.undo],
redo: ['_se_command_redo', lang.toolbar.redo + '<span class="se-shortcut">' + (shortcutsDisable.indexOf('undo') > -1 ? '' : cmd + '+<span class="se-shortcut-key">Y</span> / ' + cmd + addShift + '+<span class="se-shortcut-key">Z</span>') + '</span>', 'redo', '', icons.redo],
preview: ['se-resizing-enabled', lang.toolbar.preview, 'preview', '', icons.preview],
print: ['se-resizing-enabled', lang.toolbar.print, 'print', '', icons.print],
save: ['_se_command_save se-resizing-enabled', lang.toolbar.save + '<span class="se-shortcut">' + (shortcutsDisable.indexOf('save') > -1 ? '' : cmd + '+<span class="se-shortcut-key">S</span>') + '</span>', 'save', '', icons.save],
Expand Down
60 changes: 33 additions & 27 deletions src/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
*/
moreLayerOff: function() {
if (this._moreLayerActiveButton) {
(context.element.toolbar.querySelector('.' + this._moreLayerActiveButton.getAttribute('data-command'))).style.display = 'none';
const layer = context.element.toolbar.querySelector('.' + this._moreLayerActiveButton.getAttribute('data-command'));
layer.style.display = 'none';
util.removeClass(this._moreLayerActiveButton, 'on');
this._moreLayerActiveButton = null;
}
Expand Down Expand Up @@ -4011,19 +4012,27 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
actionCall: function (command, display, target) {
// call plugins
if (display) {
if (/more/i.test(display) && target !== this._moreLayerActiveButton) {
const layer = context.element.toolbar.querySelector('.' + command);
if (layer) {
if (this._moreLayerActiveButton) {
(context.element.toolbar.querySelector('.' + this._moreLayerActiveButton.getAttribute('data-command'))).style.display = 'none';
util.removeClass(this._moreLayerActiveButton, 'on');
if (/more/i.test(display)) {
if (target !== this._moreLayerActiveButton) {
const layer = context.element.toolbar.querySelector('.' + command);
if (layer) {
if (this._moreLayerActiveButton) this.moreLayerOff();

this._moreLayerActiveButton = target;
layer.style.display = 'block';

event._showToolbarBalloon();
event._showToolbarInline();
}
util.addClass(target, 'on');
this._moreLayerActiveButton = target;
layer.style.display = 'block';
} else {
const layer = context.element.toolbar.querySelector('.' + this._moreLayerActiveButton.getAttribute('data-command'));
if (layer) {
this.moreLayerOff();

event._showToolbarBalloon();
event._showToolbarInline();
event._showToolbarBalloon();
event._showToolbarInline();
}
}
return;
}
Expand All @@ -4033,7 +4042,7 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
return;
}

if (this.isReadOnly) return;
if (this.isReadOnly && util.arrayIncludes(this.resizingDisabledButtons, target)) return;
if (/submenu/.test(display) && (this._menuTray[command] === null || target !== this.submenuActiveButton)) {
this.callPlugin(command, this.submenuOn.bind(this, target), target);
return;
Expand All @@ -4050,17 +4059,7 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
this.commandHandler(target, command);
}

if (/more/i.test(display)) {
const layer = context.element.toolbar.querySelector('.' + this._moreLayerActiveButton.getAttribute('data-command'));
if (layer) {
util.removeClass(this._moreLayerActiveButton, 'on');
this._moreLayerActiveButton = null;
layer.style.display = 'none';

event._showToolbarBalloon();
event._showToolbarInline();
}
} else if (/submenu/.test(display)) {
if (/submenu/.test(display)) {
this.submenuOff();
} else if (!/command/.test(display)) {
this.submenuOff();
Expand Down Expand Up @@ -7619,9 +7618,7 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
context.element = newContext.element;
context.tool = newContext.tool;
if (options.iframe) context.element.wysiwyg = core._wd.body;
core._cachingButtons();
core.history._resetCachingButton();


core.activePlugins = [];
const oldCallButtons = pluginCallButtons;
pluginCallButtons = newToolbar.pluginCallButtons;
Expand All @@ -7640,8 +7637,13 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
}
}

core._cachingButtons();
core.history._resetCachingButton();

if (core.hasFocus) event._applyTagEffects();

if (core.isReadOnly) util.setDisabledButtons(true, core.resizingDisabledButtons);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need in this line if you will apply the changes from #839 pull-request


if (core._variable.isCodeView) util.addClass(core._styleCommandMap.codeView, 'active');
if (core._variable.isFullScreen) util.addClass(core._styleCommandMap.fullScreen, 'active');
if (util.hasClass(context.element.wysiwyg, 'se-show-block')) util.addClass(core._styleCommandMap.showBlocks, 'active');
Expand Down Expand Up @@ -7949,17 +7951,21 @@ export default function (context, pluginCallButtons, plugins, lang, options, _re
readOnly: function (value) {
core.isReadOnly = value;

util.setDisabledButtons(!!value, core.resizingDisabledButtons);

if (value) {
/** off menus */
core.controllersOff();
if (core.submenuActiveButton && core.submenuActiveButton.disabled) core.submenuOff();
if (core._moreLayerActiveButton && core._moreLayerActiveButton.disabled) core.moreLayerOff();
if (core.containerActiveButton && core.containerActiveButton.disabled) core.containerOff();
if (core.modalForm) core.plugins.dialog.close.call(core);

context.element.code.setAttribute("readOnly", "true");
} else {
context.element.code.removeAttribute("readOnly");
}

util.setDisabledButtons(!!value, core.resizingDisabledButtons);
if (options.codeMirrorEditor) options.codeMirrorEditor.setOption('readOnly', !!value);
},

Expand Down
8 changes: 8 additions & 0 deletions src/lib/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ declare interface util {
*/
getArrayItem(array: any[] | HTMLCollection | NodeList, validation: Function | null, multi: boolean): any[] | Node | null;

/**
* @description Check if an array contains an element
* @param {Array|HTMLCollection|NodeList} array element array
* @param {Node} element The element to find index
* @returns {Boolean}
*/
arrayIncludes(array: any[] | HTMLCollection | NodeList, element: Node): boolean;

/**
* @description Get the index of the argument value in the element array
* @param array element array
Expand Down
15 changes: 15 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,21 @@ const util = {
return !multi ? null : arr;
},

/**
* @description Check if an array contains an element
* @param {Array|HTMLCollection|NodeList} array element array
* @param {Node} element The element to check for
* @returns {Boolean}
*/
arrayIncludes: function(array, element) {
for (let i = 0; i < array.length; i++) {
if (array[i] === element) {
return true;
}
}
return false;
},

/**
* @description Get the index of the argument value in the element array
* @param {Array|HTMLCollection|NodeList} array element array
Expand Down