UNPKG

@atlaskit/editor-plugin-synced-block

Version:

SyncedBlock plugin for @atlaskit/editor-core

287 lines (282 loc) 12.4 kB
import { defaultSchema } from '@atlaskit/adf-schema/schema-default'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { copyDomNode, toDOM } from '@atlaskit/editor-common/copy-button'; import { DOMSerializer, Fragment } from '@atlaskit/editor-prosemirror/model'; import { TextSelection } from '@atlaskit/editor-prosemirror/state'; import { findSelectedNodeOfType, removeParentNodeOfType, removeSelectedNode, safeInsert } from '@atlaskit/editor-prosemirror/utils'; import { syncedBlockPluginKey } from '../pm-plugins/main'; import { canBeConvertedToSyncBlock, deferDispatch, findSyncBlock, findSyncBlockOrBodiedSyncBlock, isBodiedSyncBlockNode } from '../pm-plugins/utils/utils'; import { FLAG_ID } from '../types'; import { pasteSyncBlockHTMLContent } from './utils'; export const createSyncedBlock = ({ tr, syncBlockStore, typeAheadInsert, fireAnalyticsEvent }) => { const { schema: { nodes: { bodiedSyncBlock, paragraph } } } = tr.doc.type; // If the selection is empty, we want to insert the sync block on a new line if (tr.selection.empty) { const attrs = syncBlockStore.sourceManager.generateBodiedSyncBlockAttrs(); const paragraphNode = paragraph.createAndFill({}); const newBodiedSyncBlockNode = bodiedSyncBlock.createAndFill(attrs, paragraphNode ? [paragraphNode] : []); if (!newBodiedSyncBlockNode) { fireAnalyticsEvent === null || fireAnalyticsEvent === void 0 ? void 0 : fireAnalyticsEvent({ action: ACTION.ERROR, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_CREATE, attributes: { error: 'Create and fill for empty content failed' }, eventType: EVENT_TYPE.OPERATIONAL }); return false; } if (typeAheadInsert) { tr = typeAheadInsert(newBodiedSyncBlockNode); } else { tr = safeInsert(newBodiedSyncBlockNode)(tr).scrollIntoView(); } } else { const conversionInfo = canBeConvertedToSyncBlock(tr.selection); if (!conversionInfo) { fireAnalyticsEvent === null || fireAnalyticsEvent === void 0 ? void 0 : fireAnalyticsEvent({ action: ACTION.ERROR, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_CREATE, attributes: { error: 'Content cannot be converted to sync block' }, eventType: EVENT_TYPE.OPERATIONAL }); return false; } const attrs = syncBlockStore.sourceManager.generateBodiedSyncBlockAttrs(); const newBodiedSyncBlockNode = bodiedSyncBlock.createAndFill(attrs, conversionInfo.contentToInclude); if (!newBodiedSyncBlockNode) { fireAnalyticsEvent === null || fireAnalyticsEvent === void 0 ? void 0 : fireAnalyticsEvent({ action: ACTION.ERROR, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_CREATE, attributes: { error: 'Create and fill for content failed' }, eventType: EVENT_TYPE.OPERATIONAL }); return false; } tr.replaceWith(conversionInfo.from, conversionInfo.to, newBodiedSyncBlockNode).scrollIntoView(); // set selection to the start of the previous selection for the position taken up by the start of the new synced block tr.setSelection(TextSelection.create(tr.doc, conversionInfo.from)); } return tr; }; export const copySyncedBlockReferenceToClipboardEditorCommand = (syncBlockStore, inputMethod, api) => ({ tr }) => { if (copySyncedBlockReferenceToClipboardInternal(tr.doc.type.schema, tr.selection, syncBlockStore, inputMethod, api)) { return tr; } return null; }; export const copySyncedBlockReferenceToClipboard = (syncBlockStore, inputMethod, api) => (state, _dispatch, _view) => copySyncedBlockReferenceToClipboardInternal(state.tr.doc.type.schema, state.tr.selection, syncBlockStore, inputMethod, api); const copySyncedBlockReferenceToClipboardInternal = (schema, selection, syncBlockStore, inputMethod, api) => { const syncBlockFindResult = findSyncBlockOrBodiedSyncBlock(schema, selection); if (!syncBlockFindResult) { var _api$analytics, _api$analytics$action; api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : (_api$analytics$action = _api$analytics.actions) === null || _api$analytics$action === void 0 ? void 0 : _api$analytics$action.fireAnalyticsEvent({ eventType: EVENT_TYPE.OPERATIONAL, action: ACTION.ERROR, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_COPY, attributes: { error: 'No sync block found in selection', inputMethod } }); return false; } const isBodiedSyncBlock = isBodiedSyncBlockNode(syncBlockFindResult.node, schema.nodes.bodiedSyncBlock); let referenceSyncBlockNode = null; if (isBodiedSyncBlock) { const { nodes: { syncBlock } } = schema; // create sync block reference node referenceSyncBlockNode = syncBlock.createAndFill({ resourceId: syncBlockStore.referenceManager.generateResourceIdForReference(syncBlockFindResult.node.attrs.resourceId) }); if (!referenceSyncBlockNode) { var _api$analytics2, _api$analytics2$actio; api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : (_api$analytics2$actio = _api$analytics2.actions) === null || _api$analytics2$actio === void 0 ? void 0 : _api$analytics2$actio.fireAnalyticsEvent({ eventType: EVENT_TYPE.OPERATIONAL, action: ACTION.ERROR, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_COPY, attributes: { error: 'Failed to create reference sync block node', resourceId: syncBlockFindResult.node.attrs.resourceId, inputMethod } }); return false; } } else { referenceSyncBlockNode = syncBlockFindResult.node; } if (!referenceSyncBlockNode) { var _api$analytics3, _api$analytics3$actio; api === null || api === void 0 ? void 0 : (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : (_api$analytics3$actio = _api$analytics3.actions) === null || _api$analytics3$actio === void 0 ? void 0 : _api$analytics3$actio.fireAnalyticsEvent({ eventType: EVENT_TYPE.OPERATIONAL, action: ACTION.ERROR, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_COPY, attributes: { error: 'No reference sync block node available', inputMethod } }); return false; } const domNode = toDOM(referenceSyncBlockNode, schema); copyDomNode(domNode, referenceSyncBlockNode.type, selection); deferDispatch(() => { api === null || api === void 0 ? void 0 : api.core.actions.execute(({ tr }) => { var _api$analytics4, _api$analytics4$actio; api === null || api === void 0 ? void 0 : (_api$analytics4 = api.analytics) === null || _api$analytics4 === void 0 ? void 0 : (_api$analytics4$actio = _api$analytics4.actions) === null || _api$analytics4$actio === void 0 ? void 0 : _api$analytics4$actio.fireAnalyticsEvent({ eventType: EVENT_TYPE.OPERATIONAL, action: ACTION.COPIED, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_COPY, attributes: { resourceId: referenceSyncBlockNode.attrs.resourceId, inputMethod } }); return tr.setMeta(syncedBlockPluginKey, { activeFlag: { id: FLAG_ID.SYNC_BLOCK_COPIED } }); }); }); return true; }; export const editSyncedBlockSource = (syncBlockStore, api) => (state, dispatch, _view) => { var _syncBlock$node, _syncBlock$node$attrs; const syncBlock = findSyncBlock(state.schema, state.selection); const resourceId = syncBlock === null || syncBlock === void 0 ? void 0 : (_syncBlock$node = syncBlock.node) === null || _syncBlock$node === void 0 ? void 0 : (_syncBlock$node$attrs = _syncBlock$node.attrs) === null || _syncBlock$node$attrs === void 0 ? void 0 : _syncBlock$node$attrs.resourceId; if (!resourceId) { return false; } const syncBlockURL = syncBlockStore.referenceManager.getSyncBlockURL(resourceId); if (syncBlockURL) { var _api$analytics5; api === null || api === void 0 ? void 0 : (_api$analytics5 = api.analytics) === null || _api$analytics5 === void 0 ? void 0 : _api$analytics5.actions.fireAnalyticsEvent({ eventType: EVENT_TYPE.OPERATIONAL, action: ACTION.SYNCED_BLOCK_EDIT_SOURCE, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_SOURCE_URL, attributes: { resourceId: resourceId } }); window.open(syncBlockURL, '_blank'); } else { var _api$analytics6, _api$analytics6$actio; const { tr } = state; api === null || api === void 0 ? void 0 : (_api$analytics6 = api.analytics) === null || _api$analytics6 === void 0 ? void 0 : (_api$analytics6$actio = _api$analytics6.actions) === null || _api$analytics6$actio === void 0 ? void 0 : _api$analytics6$actio.attachAnalyticsEvent({ eventType: EVENT_TYPE.OPERATIONAL, action: ACTION.ERROR, actionSubject: ACTION_SUBJECT.SYNCED_BLOCK, actionSubjectId: ACTION_SUBJECT_ID.SYNCED_BLOCK_SOURCE_URL, attributes: { error: 'No URL resolved for synced block' } })(tr); dispatch === null || dispatch === void 0 ? void 0 : dispatch(tr); } return true; }; export const removeSyncedBlock = api => (state, dispatch, _view) => { const { schema: { nodes }, tr } = state; if (!dispatch) { return false; } let removeTr = tr; if (findSelectedNodeOfType(nodes.syncBlock)(tr.selection) || findSelectedNodeOfType(nodes.bodiedSyncBlock)(tr.selection)) { removeTr = removeSelectedNode(tr); } else { removeTr = removeParentNodeOfType(nodes.bodiedSyncBlock)(tr); } if (!removeTr) { return false; } dispatch(removeTr); api === null || api === void 0 ? void 0 : api.core.actions.focus(); return true; }; export const removeSyncedBlockAtPos = (api, pos) => { api === null || api === void 0 ? void 0 : api.core.actions.execute(({ tr }) => { const node = tr.doc.nodeAt(pos); if ((node === null || node === void 0 ? void 0 : node.type.name) === 'syncBlock') { var _node$nodeSize; return tr.replace(pos, pos + ((_node$nodeSize = node === null || node === void 0 ? void 0 : node.nodeSize) !== null && _node$nodeSize !== void 0 ? _node$nodeSize : 0)); } return tr; }); }; /** * Deletes (bodied)SyncBlock node and paste its content to the editor */ export const unsync = (storeManager, isBodiedSyncBlock, view) => { var _storeManager$referen, _storeManager$referen2; if (!view) { return false; } const { state } = view; const syncBlock = findSyncBlockOrBodiedSyncBlock(state.schema, state.selection); if (!syncBlock) { return false; } if (isBodiedSyncBlock) { const content = syncBlock === null || syncBlock === void 0 ? void 0 : syncBlock.node.content; const { tr } = state; tr.replaceWith(syncBlock.pos, syncBlock.pos + syncBlock.node.nodeSize, content).setMeta('deletionReason', 'source-block-unsynced'); view.dispatch(tr); return true; } // handle syncBlock unsync const syncBlockContent = (_storeManager$referen = storeManager.referenceManager.getFromCache(syncBlock.node.attrs.resourceId)) === null || _storeManager$referen === void 0 ? void 0 : (_storeManager$referen2 = _storeManager$referen.data) === null || _storeManager$referen2 === void 0 ? void 0 : _storeManager$referen2.content; if (!syncBlockContent) { return false; } // use defaultSchema for serialization so we can serialize any type of nodes and marks despite current editor's schema might not allow it const contentFragment = Fragment.fromJSON(defaultSchema, syncBlockContent); const contentDOM = DOMSerializer.fromSchema(defaultSchema).serializeFragment(contentFragment); return pasteSyncBlockHTMLContent(contentDOM, view); };