@atlaskit/editor-plugin-floating-toolbar
Version:
Floating toolbar plugin for @atlaskit/editor-core
69 lines (66 loc) • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.copyNode = void 0;
var _browser = require("@atlaskit/editor-common/browser");
var _clipboard = require("@atlaskit/editor-common/clipboard");
var _copyButton = require("@atlaskit/editor-common/copy-button");
var _state = require("@atlaskit/editor-prosemirror/state");
var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
var copyNode = exports.copyNode = function copyNode(nodeType, editorAnalyticsApi, inputMethod) {
return function (_ref) {
var tr = _ref.tr;
// const { tr, schema } = state;
// This command should only be triggered by the Copy button in the floating toolbar
// which is only visible when selection is inside the target node
var contentNodeWithPos = (0, _copyButton.getSelectedNodeOrNodeParentByNodeType)({
nodeType: nodeType,
selection: tr.selection
});
if (!contentNodeWithPos) {
return tr;
}
var schema = tr.doc.type.schema;
var copyToClipboardTr = tr;
var domNode = (0, _copyButton.toDOM)(contentNodeWithPos.node, schema);
if ((0, _experiments.editorExperiment)('platform_editor_block_menu', true)) {
(0, _copyButton.copyDomNode)(domNode, contentNodeWithPos.node.type, tr.selection);
} else {
if (domNode) {
var div = document.createElement('div');
var browser = (0, _browser.getBrowserInfo)();
div.appendChild(domNode);
// if copying inline content
if (contentNodeWithPos.node.type.inlineContent) {
// The "1 1" refers to the start and end depth of the slice
// since we're copying the text inside a paragraph, it will always be 1 1
// https://github.com/ProseMirror/prosemirror-view/blob/master/src/clipboard.ts#L32
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
div.firstChild.setAttribute('data-pm-slice', '1 1 []');
} else {
// The "0 0" refers to the start and end depth of the slice
// since we're copying the block node only, it will always be 0 0
// https://github.com/ProseMirror/prosemirror-view/blob/master/src/clipboard.ts#L32
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
div.firstChild.setAttribute('data-pm-slice', '0 0 []');
}
// ED-17083 safari seems have bugs for extension copy because exntension do not have a child text(innerText) and it will not recognized as html in clipboard, this could be merge into one if this extension fixed children issue or safari fix the copy bug
// MEX-2528 safari has a bug related to the mediaSingle node with border or link. The image tag within the clipboard is not recognized as HTML when using the ClipboardItem API. To address this, we have to switch to ClipboardPolyfill
if (browser.safari && tr.selection instanceof _state.NodeSelection && (tr.selection.node.type === schema.nodes.extension || tr.selection.node.type === schema.nodes.mediaSingle)) {
(0, _clipboard.copyHTMLToClipboardPolyfill)(div);
} else {
(0, _clipboard.copyHTMLToClipboard)(div);
}
}
}
if (editorAnalyticsApi) {
var analyticsPayload = (0, _clipboard.getNodeCopiedAnalyticsPayload)(contentNodeWithPos.node, inputMethod);
editorAnalyticsApi.attachAnalyticsEvent(analyticsPayload)(copyToClipboardTr);
}
copyToClipboardTr.setMeta('scrollIntoView', false);
return copyToClipboardTr;
};
};