Mercurial Hosting > sceditor
diff src/sceditor.js @ 28:f227fdfebded
split onEvent
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 11 Aug 2022 00:04:48 -0600 |
parents | 8165b83907af |
children | ea32a44b5a6e |
line wrap: on
line diff
--- a/src/sceditor.js Wed Aug 10 23:48:47 2022 -0600 +++ b/src/sceditor.js Thu Aug 11 00:04:48 2022 -0600 @@ -288,29 +288,26 @@ * @see offEvent() */ // eslint-disable-next-line max-params - function onEvent(node, events, selector, fn, capture) { + + function onEvent1(node, events, selector, fn, capture) { events.split(' ').forEach(function (event) { - var handler; - - if (isString(selector)) { - handler = fn['_sce-event-' + event + selector] || function (e) { - var target = e.target; - while (target && target !== node) { - if (is(target, selector)) { - fn(target, e); - return; - } - - target = target.parentNode; + let handler = fn['_sce-event-' + event + selector] || function (e) { + var target = e.target; + while (target && target !== node) { + if (is(target, selector)) { + fn(target, e); + return; } - }; - - fn['_sce-event-' + event + selector] = handler; - } else { - handler = selector; - capture = fn; - } - + target = target.parentNode; + } + }; + fn['_sce-event-' + event + selector] = handler; + node.addEventListener(event, handler, capture || false); + }); + } + + function onEvent2(node, events, handler, capture) { + events.split(' ').forEach(function (event) { node.addEventListener(event, handler, capture || false); }); } @@ -2043,7 +2040,7 @@ _dropDown: function (editor, caller, callback) { var content = createElement('div'); - onEvent(content, 'click', 'a', function (target, e) { + onEvent1(content, 'click', 'a', function (target, e) { callback(data(target, 'font')); editor.closeDropDown(true); e.preventDefault(); @@ -2070,7 +2067,7 @@ _dropDown: function (editor, caller, callback) { var content = createElement('div'); - onEvent(content, 'click', 'a', function (target, e) { + onEvent1(content, 'click', 'a', function (target, e) { callback(data(target, 'size')); editor.closeDropDown(true); e.preventDefault(); @@ -2118,7 +2115,7 @@ appendChild(content, parseHTML(cmd._htmlCache)); - onEvent(content, 'click', 'a', function (target, e) { + onEvent1(content, 'click', 'a', function (target, e) { callback(data(target, 'color')); editor.closeDropDown(true); e.preventDefault(); @@ -2178,7 +2175,7 @@ insert: editor._('Insert') }, true)); - onEvent(content, 'click', '.button', function (target, e) { + onEvent1(content, 'click', '.button', function (target, e) { val = find(content, '#txt')[0].value; if (val) { @@ -2290,7 +2287,7 @@ insert: editor._('Insert') }, true)); - onEvent(content, 'click', '.button', function (target, e) { + onEvent1(content, 'click', '.button', function (target, e) { var rows = Number(find(content, '#rows')[0].value), cols = Number(find(content, '#cols')[0].value), html = '<table>'; @@ -2354,7 +2351,7 @@ urlInput.value = selected; - onEvent(content, 'click', '.button', function (target, e) { + onEvent1(content, 'click', '.button', function (target, e) { if (urlInput.value) { cb( urlInput.value, @@ -2408,7 +2405,7 @@ insert: editor._('Insert') }, true)); - onEvent(content, 'click', '.button', function (target, e) { + onEvent1(content, 'click', '.button', function (target, e) { var email = find(content, '#email')[0].value; if (email) { @@ -2465,8 +2462,8 @@ e.preventDefault(); } - onEvent(content, 'click', '.button', insertUrl); - onEvent(content, 'keypress', function (e) { + onEvent1(content, 'click', '.button', insertUrl); + onEvent2(content, 'keypress', function (e) { // 13 = enter key if (e.which === 13 && linkInput.value) { insertUrl(e); @@ -2564,7 +2561,7 @@ perLine = Math.sqrt(Object.keys(emoticons).length); - onEvent(content, 'click', 'img', function (target, e) { + onEvent1(content, 'click', 'img', function (target, e) { editor.insert(startSpace + attr(target, 'alt') + endSpace, null, false).closeDropDown(true); @@ -2592,7 +2589,7 @@ appendChild(moreLink, document.createTextNode(editor._('More'))); - onEvent(moreLink, 'click', function (e) { + onEvent2(moreLink, 'click', function (e) { editor.createDropDown( caller, 'more-emoticons', createContent(true) ); @@ -2625,7 +2622,7 @@ insert: editor._('Insert') }, true)); - onEvent(content, 'click', '.button', function (target, e) { + onEvent1(content, 'click', '.button', function (target, e) { var val = find(content, '#link')[0].value; var idMatch = val.match(/(?:v=|v\/|embed\/|youtu.be\/)?([a-zA-Z0-9_-]{11})/); var timeMatch = val.match(/[&|?](?:star)?t=((\d+[hms]?){1,3})/); @@ -4541,7 +4538,7 @@ format.onReady(base); } }; - onEvent(globalWin, 'load', loaded); + onEvent2(globalWin, 'load', loaded); if (globalDoc.readyState === 'complete') { loaded(); } @@ -4630,7 +4627,7 @@ // iframe overflow fix for iOS if (ios) { height(wysiwygBody, '100%'); - onEvent(wysiwygBody, 'touchend', base.focus); + onEvent2(wysiwygBody, 'touchend', base.focus); } var tabIndex = attr(original, 'tabindex'); @@ -4659,8 +4656,8 @@ initOptions = function () { // auto-update original textbox on blur if option set to true if (options.autoUpdate) { - onEvent(wysiwygBody, 'blur', autoUpdate); - onEvent(sourceEditor, 'blur', autoUpdate); + onEvent2(wysiwygBody, 'blur', autoUpdate); + onEvent2(sourceEditor, 'blur', autoUpdate); } if (options.rtl === null) { @@ -4671,8 +4668,8 @@ if (options.autoExpand) { // Need to update when images (or anything else) loads - onEvent(wysiwygBody, 'load', autoExpand, EVENT_CAPTURE); - onEvent(wysiwygBody, 'input keyup', autoExpand); + onEvent2(wysiwygBody, 'load', autoExpand, EVENT_CAPTURE); + onEvent2(wysiwygBody, 'input keyup', autoExpand); } if (options.resizeEnabled) { @@ -4696,55 +4693,55 @@ 'selectionchange' : 'keyup focus blur contextmenu mouseup touchend click'; - onEvent(globalDoc, 'click', handleDocumentClick); + onEvent2(globalDoc, 'click', handleDocumentClick); if (form) { - onEvent(form, 'reset', handleFormReset); - onEvent(form, 'submit', base.updateOriginal, EVENT_CAPTURE); - } - - onEvent(window, 'pagehide', base.updateOriginal); - onEvent(window, 'pageshow', handleFormReset); - onEvent(wysiwygBody, 'keypress', handleKeyPress); - onEvent(wysiwygBody, 'keydown', handleKeyDown); - onEvent(wysiwygBody, 'keydown', handleBackSpace); - onEvent(wysiwygBody, 'keyup', appendNewLine); - onEvent(wysiwygBody, 'blur', valueChangedBlur); - onEvent(wysiwygBody, 'keyup', valueChangedKeyUp); - onEvent(wysiwygBody, 'paste', handlePasteEvt); - onEvent(wysiwygBody, 'cut copy', handleCutCopyEvt); - onEvent(wysiwygBody, compositionEvents, handleComposition); - onEvent(wysiwygBody, checkSelectionEvents, checkSelectionChanged); - onEvent(wysiwygBody, eventsToForward, handleEvent); + onEvent2(form, 'reset', handleFormReset); + onEvent2(form, 'submit', base.updateOriginal, EVENT_CAPTURE); + } + + onEvent2(window, 'pagehide', base.updateOriginal); + onEvent2(window, 'pageshow', handleFormReset); + onEvent2(wysiwygBody, 'keypress', handleKeyPress); + onEvent2(wysiwygBody, 'keydown', handleKeyDown); + onEvent2(wysiwygBody, 'keydown', handleBackSpace); + onEvent2(wysiwygBody, 'keyup', appendNewLine); + onEvent2(wysiwygBody, 'blur', valueChangedBlur); + onEvent2(wysiwygBody, 'keyup', valueChangedKeyUp); + onEvent2(wysiwygBody, 'paste', handlePasteEvt); + onEvent2(wysiwygBody, 'cut copy', handleCutCopyEvt); + onEvent2(wysiwygBody, compositionEvents, handleComposition); + onEvent2(wysiwygBody, checkSelectionEvents, checkSelectionChanged); + onEvent2(wysiwygBody, eventsToForward, handleEvent); if (options.emoticonsCompat && globalWin.getSelection) { - onEvent(wysiwygBody, 'keyup', emoticonsCheckWhitespace); - } - - onEvent(wysiwygBody, 'blur', function () { + onEvent2(wysiwygBody, 'keyup', emoticonsCheckWhitespace); + } + + onEvent2(wysiwygBody, 'blur', function () { if (!base.val()) { addClass(wysiwygBody, 'placeholder'); } }); - onEvent(wysiwygBody, 'focus', function () { + onEvent2(wysiwygBody, 'focus', function () { removeClass(wysiwygBody, 'placeholder'); }); - onEvent(sourceEditor, 'blur', valueChangedBlur); - onEvent(sourceEditor, 'keyup', valueChangedKeyUp); - onEvent(sourceEditor, 'keydown', handleKeyDown); - onEvent(sourceEditor, compositionEvents, handleComposition); - onEvent(sourceEditor, eventsToForward, handleEvent); - - onEvent(wysiwygDocument, 'mousedown', handleMouseDown); - onEvent(wysiwygDocument, checkSelectionEvents, checkSelectionChanged); - onEvent(wysiwygDocument, 'keyup', appendNewLine); - - onEvent(editorContainer, 'selectionchanged', checkNodeChanged); - onEvent(editorContainer, 'selectionchanged', updateActiveButtons); + onEvent2(sourceEditor, 'blur', valueChangedBlur); + onEvent2(sourceEditor, 'keyup', valueChangedKeyUp); + onEvent2(sourceEditor, 'keydown', handleKeyDown); + onEvent2(sourceEditor, compositionEvents, handleComposition); + onEvent2(sourceEditor, eventsToForward, handleEvent); + + onEvent2(wysiwygDocument, 'mousedown', handleMouseDown); + onEvent2(wysiwygDocument, checkSelectionEvents, checkSelectionChanged); + onEvent2(wysiwygDocument, 'keyup', appendNewLine); + + onEvent2(editorContainer, 'selectionchanged', checkNodeChanged); + onEvent2(editorContainer, 'selectionchanged', updateActiveButtons); // Custom events to forward - onEvent( + onEvent2( editorContainer, 'selectionchanged valuechanged nodechanged pasteraw paste', handleEvent @@ -4799,7 +4796,7 @@ button._sceTxtMode = !!command.txtExec; button._sceWysiwygMode = !!command.exec; toggleClass(button, 'disabled', !command.exec); - onEvent(button, 'click', function (e) { + onEvent2(button, 'click', function (e) { if (!hasClass(button, 'disabled')) { handleCommand(button, command); } @@ -4808,7 +4805,7 @@ e.preventDefault(); }); // Prevent editor losing focus when button clicked - onEvent(button, 'mousedown', function (e) { + onEvent2(button, 'mousedown', function (e) { base.closeDropDown(); e.preventDefault(); }); @@ -4954,7 +4951,7 @@ appendChild(editorContainer, cover); hide(cover); - onEvent(grip, 'touchstart mousedown', function (e) { + onEvent2(grip, 'touchstart mousedown', function (e) { // iOS uses window.event if (e.type === 'touchstart') { e = globalWin.event; @@ -4971,8 +4968,8 @@ addClass(editorContainer, 'resizing'); show(cover); - onEvent(globalDoc, moveEvents, mouseMoveFunc); - onEvent(globalDoc, endEvents, mouseUpFunc); + onEvent2(globalDoc, moveEvents, mouseMoveFunc); + onEvent2(globalDoc, endEvents, mouseUpFunc); e.preventDefault(); }); @@ -5493,7 +5490,7 @@ css(dropdown, dropDownCss); appendChild(dropdown, content); appendChild(editorContainer, dropdown); - onEvent(dropdown, 'click focusin', function (e) { + onEvent2(dropdown, 'click focusin', function (e) { // stop clicks within the dropdown from being handled e.stopPropagation(); }); @@ -7078,7 +7075,7 @@ options.emoticonsEnabled = enable; if (enable) { - onEvent(wysiwygBody, 'keypress', emoticonsKeyPress); + onEvent2(wysiwygBody, 'keypress', emoticonsKeyPress); if (!base.sourceMode()) { rangeHelper.saveRange();