UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

212 lines (210 loc) 11.6 kB
import { convertToInlineCss } from '@atlaskit/editor-common/lazy-node-view'; import { blockControlsMessages as messages } from '@atlaskit/editor-common/messages'; import { VanillaTooltip } from '@atlaskit/editor-common/vanilla-tooltip'; import { DOMSerializer } from '@atlaskit/editor-prosemirror/model'; import { TextSelection } from '@atlaskit/editor-prosemirror/state'; import { findParentNode, findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils'; import { CellSelection } from '@atlaskit/editor-tables/cell-selection'; import { isInTextSelection, isNestedNodeSelected, isNonEditableBlock, isSelectionInNode } from '../ui/utils/document-checks'; import { createNewLine } from '../ui/utils/editor-commands'; import { calculatePosition } from './quick-insert-calculate-position'; // Based on platform/packages/design-system/icon/svgs/utility/add.svg const plusButtonDOM = ['http://www.w3.org/2000/svg svg', { width: '12', height: '12', fill: 'none', viewBox: '0 0 12 12', style: 'pointer-events: none;' }, ['http://www.w3.org/2000/svg path', { fill: 'currentcolor', 'fill-rule': 'evenodd', d: 'M5.25 6.75V11h1.5V6.75H11v-1.5H6.75V1h-1.5v4.25H1v1.5z', 'clip-rule': 'evenodd', style: 'pointer-events: none;' }]]; const vanillaQuickInsert = ({ view, getPos, rootNodeType, anchorRectCache, anchorName, rootAnchorName, api }) => { var _api$featureFlags$sha, _api$featureFlags, _api$featureFlags$sha2; return ['div', { style: convertToInlineCss({ position: 'absolute', ...calculatePosition({ rootAnchorName, anchorName, view, getPos, rootNodeType: rootNodeType, macroInteractionUpdates: (_api$featureFlags$sha = api === null || api === void 0 ? void 0 : (_api$featureFlags = api.featureFlags) === null || _api$featureFlags === void 0 ? void 0 : (_api$featureFlags$sha2 = _api$featureFlags.sharedState.currentState()) === null || _api$featureFlags$sha2 === void 0 ? void 0 : _api$featureFlags$sha2.macroInteractionUpdates) !== null && _api$featureFlags$sha !== void 0 ? _api$featureFlags$sha : false, anchorRectCache }) }) }, ['button', { class: 'blocks-quick-insert-button', 'data-testid': 'editor-quick-insert-button' }, plusButtonDOM]]; }; /** * Create a Node which contains the quick insert button */ export const createVanillaButton = props => { var _props$api$typeAhead, _props$api$typeAhead$, _props$api$blockContr, _props$api$blockContr2, _props$api$blockContr3, _props$api$blockContr4, _props$api$blockContr5, _props$api$blockContr6, _props$api$blockContr7, _props$api$editorView, _props$api$editorView2, _props$api$typeAhead2, _props$api$blockContr8; const { dom } = DOMSerializer.renderSpec(document, vanillaQuickInsert(props)); if (dom instanceof HTMLElement) { const button = dom.querySelector('button[data-testid="editor-quick-insert-button"]'); if (button instanceof HTMLButtonElement) { button.onclick = () => handleQuickInsert(props); const tooltip = new VanillaTooltip(button, props.formatMessage(messages.insert), 'quick-insert-button-tooltip', 'blocks-quick-insert-tooltip'); props.cleanupCallbacks.push(() => { tooltip.destroy(); }); } } // Dynamically control the visibility of the node let isTypeAheadOpen = (_props$api$typeAhead = props.api.typeAhead) === null || _props$api$typeAhead === void 0 ? void 0 : (_props$api$typeAhead$ = _props$api$typeAhead.sharedState.currentState()) === null || _props$api$typeAhead$ === void 0 ? void 0 : _props$api$typeAhead$.isOpen; let isEditing = (_props$api$blockContr = props.api.blockControls) === null || _props$api$blockContr === void 0 ? void 0 : (_props$api$blockContr2 = _props$api$blockContr.sharedState.currentState()) === null || _props$api$blockContr2 === void 0 ? void 0 : _props$api$blockContr2.isEditing; let hoverSide = (_props$api$blockContr3 = props.api.blockControls) === null || _props$api$blockContr3 === void 0 ? void 0 : (_props$api$blockContr4 = _props$api$blockContr3.sharedState.currentState()) === null || _props$api$blockContr4 === void 0 ? void 0 : _props$api$blockContr4.hoverSide; let rightSideControlsEnabled = (_props$api$blockContr5 = (_props$api$blockContr6 = props.api.blockControls) === null || _props$api$blockContr6 === void 0 ? void 0 : (_props$api$blockContr7 = _props$api$blockContr6.sharedState.currentState()) === null || _props$api$blockContr7 === void 0 ? void 0 : _props$api$blockContr7.rightSideControlsEnabled) !== null && _props$api$blockContr5 !== void 0 ? _props$api$blockContr5 : false; let editorViewMode = (_props$api$editorView = props.api.editorViewMode) === null || _props$api$editorView === void 0 ? void 0 : (_props$api$editorView2 = _props$api$editorView.sharedState.currentState()) === null || _props$api$editorView2 === void 0 ? void 0 : _props$api$editorView2.mode; const changeDOMVisibility = () => { if (!(dom instanceof HTMLElement)) { return; } const isViewMode = editorViewMode === 'view'; const shouldRestrictBySide = rightSideControlsEnabled && !isViewMode; // Only restrict by side when hoverSide is known. When undefined, show quick insert. const sideHidden = shouldRestrictBySide && hoverSide !== undefined ? hoverSide !== 'left' : false; if (isTypeAheadOpen || isEditing || sideHidden) { dom.classList.add('blocks-quick-insert-invisible-container'); dom.classList.remove('blocks-quick-insert-visible-container'); } else { dom.classList.add('blocks-quick-insert-visible-container'); dom.classList.remove('blocks-quick-insert-invisible-container'); } }; changeDOMVisibility(); props.cleanupCallbacks.push((_props$api$typeAhead2 = props.api.typeAhead) === null || _props$api$typeAhead2 === void 0 ? void 0 : _props$api$typeAhead2.sharedState.onChange(({ nextSharedState }) => { isTypeAheadOpen = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.isOpen; changeDOMVisibility(); })); props.cleanupCallbacks.push((_props$api$blockContr8 = props.api.blockControls) === null || _props$api$blockContr8 === void 0 ? void 0 : _props$api$blockContr8.sharedState.onChange(({ nextSharedState }) => { var _nextSharedState$righ; isEditing = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.isEditing; hoverSide = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.hoverSide; rightSideControlsEnabled = (_nextSharedState$righ = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.rightSideControlsEnabled) !== null && _nextSharedState$righ !== void 0 ? _nextSharedState$righ : false; changeDOMVisibility(); })); // Only subscribe to view mode when right-side controls are enabled (editorViewMode affects side restriction) if (rightSideControlsEnabled) { var _props$api$editorView3, _props$api$editorView4, _props$api$editorView5; const unsubscribeViewMode = (_props$api$editorView3 = props.api.editorViewMode) === null || _props$api$editorView3 === void 0 ? void 0 : (_props$api$editorView4 = (_props$api$editorView5 = _props$api$editorView3.sharedState).onChange) === null || _props$api$editorView4 === void 0 ? void 0 : _props$api$editorView4.call(_props$api$editorView5, ({ nextSharedState }) => { editorViewMode = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.mode; changeDOMVisibility(); }); if (unsubscribeViewMode) { props.cleanupCallbacks.push(unsubscribeViewMode); } } return dom; }; const TEXT_PARENT_TYPES = ['paragraph', 'heading', 'blockquote', 'taskItem', 'decisionItem']; const handleQuickInsert = ({ api, view, getPos }) => { var _api$quickInsert; // if the selection is not within the node this decoration is rendered at // then insert a newline and trigger quick insert const start = getPos(); if (start !== undefined) { // if the selection is not within the node this decoration is rendered at // or the node is non-editable, then insert a newline and trigger quick insert const isSelectionInsideNode = isSelectionInNode(start, view); if (!isSelectionInsideNode || isNonEditableBlock(start, view)) { api.core.actions.execute(createNewLine(start)); } const { codeBlock } = view.state.schema.nodes; const { selection } = view.state; const codeBlockParentNode = findParentNodeOfType(codeBlock)(selection); if (codeBlockParentNode) { // Slash command is not meant to be triggered inside code block, hence always insert slash in a new line following api.core.actions.execute(createNewLine(codeBlockParentNode.pos)); } else if (isSelectionInsideNode) { // text or element with be deselected and the / added immediately after the paragraph // unless the selection is empty const currentSelection = view.state.selection; if (isInTextSelection(view) && currentSelection.from !== currentSelection.to) { const currentParagraphNode = findParentNode(node => TEXT_PARENT_TYPES.includes(node.type.name))(currentSelection); if (currentParagraphNode) { const newPos = //if the current selection is selected from right to left, then set the selection to the start of the paragraph currentSelection.anchor === currentSelection.to ? currentParagraphNode.pos : currentParagraphNode.pos + currentParagraphNode.node.nodeSize - 1; api.core.actions.execute(({ tr }) => { tr.setSelection(TextSelection.create(view.state.selection.$from.doc, newPos)); return tr; }); } } if (isNestedNodeSelected(view)) { // if the nested selected node is non-editable, then insert a newline below the selected node if (isNonEditableBlock(view.state.selection.from, view)) { api.core.actions.execute(createNewLine(view.state.selection.from)); } else { // otherwise need to force the selection to be at the start of the node, because // prosemirror is keeping it as NodeSelection for nested nodes. Do this to keep it // consistent NodeSelection for root level nodes. api.core.actions.execute(({ tr }) => { createNewLine(view.state.selection.from)({ tr }); tr.setSelection(TextSelection.create(tr.doc, view.state.selection.from)); return tr; }); } } if (currentSelection instanceof CellSelection) { // find the last inline position in the selection const lastInlinePosition = TextSelection.near(view.state.selection.$to, -1); lastInlinePosition && api.core.actions.execute(({ tr }) => { if (!(lastInlinePosition instanceof TextSelection)) { // this will create a new line after the node createNewLine(lastInlinePosition.from)({ tr }); // this will find the next valid text position after the node tr.setSelection(TextSelection.create(tr.doc, lastInlinePosition.to)); } else { tr.setSelection(lastInlinePosition); } return tr; }); } } } (_api$quickInsert = api.quickInsert) === null || _api$quickInsert === void 0 ? void 0 : _api$quickInsert.actions.openTypeAhead('blockControl', true); };