import { Assertions, Chain, GeneralSteps, Logger, Pipeline } from '@ephox/agar'; import { Focus, Hierarchy, Element } from '@ephox/sugar'; import EditorManager from 'tinymce/core/api/EditorManager'; import EditorFocus from 'tinymce/core/focus/EditorFocus'; import ViewBlock from '../../module/test/ViewBlock'; import Theme from 'tinymce/themes/modern/Theme'; import { UnitTest } from '@ephox/bedrock'; UnitTest.asynctest('browser.tinymce.core.focus.EditorFocusTest', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; const viewBlock = ViewBlock(); Theme(); const cCreateInlineEditor = function (html) { return Chain.on(function (viewBlock, next, die) { viewBlock.update(html); EditorManager.init({ selector: '.tinymce-editor', inline: true, skin_url: '/project/js/tinymce/skins/lightgray', setup (editor) { editor.on('SkinLoaded', function () { next(Chain.wrap(editor)); }); } }); }); }; const cFocusEditor = Chain.op(function (editor) { EditorFocus.focus(editor, false); }); const cFocusElement = function (elementPath) { return Chain.op(function (editor) { const element = Hierarchy.follow(Element.fromDom(editor.getBody()), elementPath).getOrDie(); element.dom().focus(); }); }; const cSetSelection = function (startPath, startOffset, endPath, endOffset) { return Chain.op(function (editor) { const startContainer = Hierarchy.follow(Element.fromDom(editor.getBody()), startPath).getOrDie(); const endContainer = Hierarchy.follow(Element.fromDom(editor.getBody()), endPath).getOrDie(); const rng = editor.dom.createRng(); rng.setStart(startContainer.dom(), startOffset); rng.setEnd(endContainer.dom(), endOffset); editor.selection.setRng(rng); }); }; const cAssertSelection = function (startPath, startOffset, endPath, endOffset) { return Chain.op(function (editor) { const startContainer = Hierarchy.follow(Element.fromDom(editor.getBody()), startPath).getOrDie(); const endContainer = Hierarchy.follow(Element.fromDom(editor.getBody()), endPath).getOrDie(); const rng = editor.selection.getRng(); Assertions.assertDomEq('Should be expected from start container', startContainer, Element.fromDom(rng.startContainer)); Assertions.assertEq('Should be expected from start offset', startOffset, rng.startOffset); Assertions.assertDomEq('Should be expected end container', endContainer, Element.fromDom(rng.endContainer)); Assertions.assertEq('Should be expected end offset', endOffset, rng.endOffset); }); }; const cAssertHasFocus = function (elementPath) { return Chain.op(function (editor) { const element = Hierarchy.follow(Element.fromDom(editor.getBody()), elementPath).getOrDie(); Assertions.assertEq('Should have focus on the editor', true, EditorFocus.hasFocus(editor)); Assertions.assertDomEq('Should be the expected activeElement', element, Focus.active().getOrDie()); }); }; const cRemoveEditor = Chain.op(function (editor) { editor.remove(); }); viewBlock.attach(); Pipeline.async({}, [ Logger.t('Focus editor', GeneralSteps.sequence([ Logger.t('Focus editor initialized on a table', Chain.asStep(viewBlock, [ cCreateInlineEditor('
| a |
a
a