@atlaskit/editor-plugin-paste
Version:
Paste plugin for @atlaskit/editor-core
54 lines (51 loc) • 2.32 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import { DOMParser as PMDOMParser } from '@atlaskit/editor-prosemirror/model';
import { expValEquals } from '@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.
*/
export function createClipboardParser(schema) {
if (!expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true)) {
return undefined;
}
var additionalRules = buildAdditionalClipboardParseRules(schema);
var baseParser = PMDOMParser.fromSchema(schema);
var customParser = new PMDOMParser(schema, [].concat(_toConsumableArray(additionalRules), _toConsumableArray(baseParser.rules)));
return customParser;
}