UNPKG

jodit

Version:

Jodit is an awesome and useful wysiwyg editor with filebrowser

79 lines (78 loc) 2.41 kB
/*! * Jodit Editor (https://xdsoft.net/jodit/) * Released under MIT see LICENSE.txt in the project root for license information. * Copyright (c) 2013-2026 Valerii Chupurnov. All rights reserved. https://xdsoft.net */ import { Dom } from "../../core/dom/index.js"; import { pluginSystem } from "../../core/global.js"; import { Config } from "../../config.js"; /** * After loading the page into the editor once the focus is set */ Config.prototype.autofocus = false; /** * Cursor position after autofocus */ Config.prototype.cursorAfterAutofocus = 'end'; /** * Save current selection on blur event */ Config.prototype.saveSelectionOnBlur = true; export function focus(editor) { if (editor.o.saveSelectionOnBlur) { editor.e .on('blur', () => { if (editor.isEditorMode()) { editor.s.save(true); } }) .on('focus', () => { if (editor.isEditorMode()) { editor.s.restore(); } }); } const focus = () => { editor.s.focus(); let textNode = null; switch (editor.o.cursorAfterAutofocus) { case 'start': textNode = Dom.first(editor.editor, node => Dom.isText(node)); break; case 'end': textNode = Dom.last(editor.editor, node => Dom.isText(node)); break; } if (textNode) { editor.s.setCursorIn(textNode, editor.o.cursorAfterAutofocus === 'start'); } }; editor.e.on('afterInit', () => { if (editor.o.autofocus) { if (editor.defaultTimeout) { editor.async.setTimeout(focus, 300); } else { focus(); } } }); editor.e.on('afterInit afterAddPlace', () => { editor.e .off(editor.editor, 'mousedown.autofocus') .on(editor.editor, 'mousedown.autofocus', (e) => { if (editor.isEditorMode() && e.target && Dom.isBlock(e.target) && !e.target.childNodes.length) { if (editor.editor === e.target) { editor.s.focus(); } else { editor.s.setCursorIn(e.target); } } }); }); } pluginSystem.add('focus', focus);