UNPKG

@atlaskit/editor-plugin-paste

Version:

Paste plugin for @atlaskit/editor-core

60 lines (57 loc) 2.6 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.createClipboardParser = createClipboardParser; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _model = require("@atlaskit/editor-prosemirror/model"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); /** * Builds additional parseDOM rules to inject into the clipboard parser. * * Adding a rule with a higher priority than the default schema rules, allows * specific HTML patterns to be parsed as a different node type than the schema * would normally choose. */ function buildAdditionalClipboardParseRules(schema) { var _panel$spec$parseDOM; var rules = []; // This means that panels with tables inside always stay as panel_c1, // but panels without tables retain standard behaviour // of choosing between panel_c1 and panel depending on the context. // e.g. panel with table pasted in expand gets lifted out, panel with normal text gets pasted inside expand var _schema$nodes = schema.nodes, panel_c1 = _schema$nodes.panel_c1, panel = _schema$nodes.panel; if (!panel_c1 || !panel) { return rules; } var panelGetAttrs = (_panel$spec$parseDOM = panel.spec.parseDOM) === null || _panel$spec$parseDOM === void 0 || (_panel$spec$parseDOM = _panel$spec$parseDOM[0]) === null || _panel$spec$parseDOM === void 0 ? void 0 : _panel$spec$parseDOM.getAttrs; rules.push({ tag: 'div[data-panel-type]', priority: 60, node: panel_c1.name, getAttrs: function getAttrs(dom) { var hasTable = dom.querySelector('[data-prosemirror-node-name="table"]') !== null; if (!hasTable) { return false; // fall through to standard panel rule } return panelGetAttrs ? panelGetAttrs(dom) : null; } }); return rules; } /** * Creates a ProseMirror plugin with a custom `clipboardParser` that extends the * schema's default parser with additional rules for handling specific paste scenarios. */ function createClipboardParser(schema) { if (!(0, _expValEquals.expValEquals)('platform_editor_nest_table_in_panel', 'isEnabled', true)) { return undefined; } var additionalRules = buildAdditionalClipboardParseRules(schema); var baseParser = _model.DOMParser.fromSchema(schema); var customParser = new _model.DOMParser(schema, [].concat((0, _toConsumableArray2.default)(additionalRules), (0, _toConsumableArray2.default)(baseParser.rules))); return customParser; }