UNPKG

jodit

Version:

Jodit is an awesome and useful wysiwyg editor with filebrowser

101 lines (100 loc) 3.77 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 { CLIPBOARD_ID, INSERT_AS_HTML, TEXT_HTML, TEXT_PLAIN } from "../../core/constants.js"; import { cached } from "../../core/decorators/cache/cache.js"; import { Dom } from "../../core/dom/dom.js"; import { pluginSystem } from "../../core/global.js"; import { getDataTransfer, stripTags } from "../../core/helpers/index.js"; import "./config.js"; /** * Clipboard plugin - cut and copy functionality */ export class clipboard { constructor() { /** @override */ this.buttons = [ { name: 'cut', group: 'clipboard' }, { name: 'copy', group: 'clipboard' }, { name: 'paste', group: 'clipboard' }, { name: 'selectall', group: 'clipboard' } ]; } init(editor) { var _a; (_a = this.buttons) === null || _a === void 0 ? void 0 : _a.forEach(btn => editor.registerButton(btn)); editor.e .off(`copy.${CLIPBOARD_ID} cut.${CLIPBOARD_ID}`) .on(`copy.${CLIPBOARD_ID} cut.${CLIPBOARD_ID}`, (event) => { var _a; const selectedText = wrapWithInlineAncestors(editor, editor.s.html); const clipboardData = getDataTransfer(event) || getDataTransfer(editor.ew) || getDataTransfer(event.originalEvent); if (clipboardData) { clipboardData.setData(TEXT_PLAIN, stripTags(selectedText)); clipboardData.setData(TEXT_HTML, selectedText); } editor.buffer.set(CLIPBOARD_ID, selectedText); editor.e.fire('pasteStack', { html: selectedText, action: editor.o.defaultActionOnPaste || INSERT_AS_HTML }); if (event.type === 'cut') { editor.s.remove(); editor.s.focus(); } event.preventDefault(); (_a = editor === null || editor === void 0 ? void 0 : editor.events) === null || _a === void 0 ? void 0 : _a.fire('afterCopy', selectedText); }); } /** @override */ destruct(editor) { var _a, _b; (_a = cached(editor, 'buffer')) === null || _a === void 0 ? void 0 : _a.set(CLIPBOARD_ID, ''); (_b = editor === null || editor === void 0 ? void 0 : editor.events) === null || _b === void 0 ? void 0 : _b.off('.' + CLIPBOARD_ID); } } /** * `Selection.html` clones only the range contents — when the selection sits * entirely inside the text of a formatted element (`<strong>te|st|</strong>`), * the clone is bare text and the formatting would be lost on paste. Native * browser copy keeps that context, so the interception must restore it: wrap * the fragment in shallow clones of the inline ancestors of the range. See #1202 */ function wrapWithInlineAncestors(editor, html) { if (!html || editor.s.isCollapsed()) { return html; } let node = editor.s.range.commonAncestorContainer; if (!Dom.isElement(node)) { node = node.parentElement; } let result = html; while (node && node !== editor.editor && Dom.isElement(node) && !Dom.isBlock(node)) { const shell = node.cloneNode(false); shell.innerHTML = result; result = shell.outerHTML; node = node.parentElement; } return result; } pluginSystem.add('clipboard', clipboard);