UNPKG

@atlaskit/editor-plugin-block-controls

Version:

Block controls plugin for @atlaskit/editor-core

205 lines (203 loc) 12.6 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } 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 var 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;' }]]; var vanillaQuickInsert = function vanillaQuickInsert(_ref) { var _api$featureFlags$sha, _api$featureFlags; var view = _ref.view, getPos = _ref.getPos, rootNodeType = _ref.rootNodeType, anchorRectCache = _ref.anchorRectCache, anchorName = _ref.anchorName, rootAnchorName = _ref.rootAnchorName, api = _ref.api; return ['div', { style: convertToInlineCss(_objectSpread({ position: 'absolute' }, calculatePosition({ rootAnchorName: rootAnchorName, anchorName: anchorName, view: view, getPos: getPos, rootNodeType: rootNodeType, macroInteractionUpdates: (_api$featureFlags$sha = api === null || api === void 0 || (_api$featureFlags = api.featureFlags) === null || _api$featureFlags === void 0 || (_api$featureFlags = _api$featureFlags.sharedState.currentState()) === null || _api$featureFlags === void 0 ? void 0 : _api$featureFlags.macroInteractionUpdates) !== null && _api$featureFlags$sha !== void 0 ? _api$featureFlags$sha : false, anchorRectCache: anchorRectCache }))) }, ['button', { class: 'blocks-quick-insert-button', 'data-testid': 'editor-quick-insert-button' }, plusButtonDOM]]; }; /** * Create a Node which contains the quick insert button */ export var createVanillaButton = function createVanillaButton(props) { var _props$api$typeAhead, _props$api$blockContr, _props$api$blockContr2, _props$api$blockContr3, _props$api$blockContr4, _props$api$editorView, _props$api$typeAhead2, _props$api$blockContr5; var _DOMSerializer$render = DOMSerializer.renderSpec(document, vanillaQuickInsert(props)), dom = _DOMSerializer$render.dom; if (dom instanceof HTMLElement) { var button = dom.querySelector('button[data-testid="editor-quick-insert-button"]'); if (button instanceof HTMLButtonElement) { button.onclick = function () { return handleQuickInsert(props); }; var tooltip = new VanillaTooltip(button, props.formatMessage(messages.insert), 'quick-insert-button-tooltip', 'blocks-quick-insert-tooltip'); props.cleanupCallbacks.push(function () { tooltip.destroy(); }); } } // Dynamically control the visibility of the node var isTypeAheadOpen = (_props$api$typeAhead = props.api.typeAhead) === null || _props$api$typeAhead === void 0 || (_props$api$typeAhead = _props$api$typeAhead.sharedState.currentState()) === null || _props$api$typeAhead === void 0 ? void 0 : _props$api$typeAhead.isOpen; var isEditing = (_props$api$blockContr = props.api.blockControls) === null || _props$api$blockContr === void 0 || (_props$api$blockContr = _props$api$blockContr.sharedState.currentState()) === null || _props$api$blockContr === void 0 ? void 0 : _props$api$blockContr.isEditing; var hoverSide = (_props$api$blockContr2 = props.api.blockControls) === null || _props$api$blockContr2 === void 0 || (_props$api$blockContr2 = _props$api$blockContr2.sharedState.currentState()) === null || _props$api$blockContr2 === void 0 ? void 0 : _props$api$blockContr2.hoverSide; var rightSideControlsEnabled = (_props$api$blockContr3 = (_props$api$blockContr4 = props.api.blockControls) === null || _props$api$blockContr4 === void 0 || (_props$api$blockContr4 = _props$api$blockContr4.sharedState.currentState()) === null || _props$api$blockContr4 === void 0 ? void 0 : _props$api$blockContr4.rightSideControlsEnabled) !== null && _props$api$blockContr3 !== void 0 ? _props$api$blockContr3 : false; var editorViewMode = (_props$api$editorView = props.api.editorViewMode) === null || _props$api$editorView === void 0 || (_props$api$editorView = _props$api$editorView.sharedState.currentState()) === null || _props$api$editorView === void 0 ? void 0 : _props$api$editorView.mode; var changeDOMVisibility = function changeDOMVisibility() { if (!(dom instanceof HTMLElement)) { return; } var isViewMode = editorViewMode === 'view'; var shouldRestrictBySide = rightSideControlsEnabled && !isViewMode; // Only restrict by side when hoverSide is known. When undefined, show quick insert. var 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(function (_ref2) { var nextSharedState = _ref2.nextSharedState; isTypeAheadOpen = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.isOpen; changeDOMVisibility(); })); props.cleanupCallbacks.push((_props$api$blockContr5 = props.api.blockControls) === null || _props$api$blockContr5 === void 0 ? void 0 : _props$api$blockContr5.sharedState.onChange(function (_ref3) { var _nextSharedState$righ; var nextSharedState = _ref3.nextSharedState; 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$editorView2, _props$api$editorView3, _props$api$editorView4; var unsubscribeViewMode = (_props$api$editorView2 = props.api.editorViewMode) === null || _props$api$editorView2 === void 0 || (_props$api$editorView3 = (_props$api$editorView4 = _props$api$editorView2.sharedState).onChange) === null || _props$api$editorView3 === void 0 ? void 0 : _props$api$editorView3.call(_props$api$editorView4, function (_ref4) { var nextSharedState = _ref4.nextSharedState; editorViewMode = nextSharedState === null || nextSharedState === void 0 ? void 0 : nextSharedState.mode; changeDOMVisibility(); }); if (unsubscribeViewMode) { props.cleanupCallbacks.push(unsubscribeViewMode); } } return dom; }; var TEXT_PARENT_TYPES = ['paragraph', 'heading', 'blockquote', 'taskItem', 'decisionItem']; var handleQuickInsert = function handleQuickInsert(_ref5) { var _api$quickInsert; var api = _ref5.api, view = _ref5.view, getPos = _ref5.getPos; // if the selection is not within the node this decoration is rendered at // then insert a newline and trigger quick insert var 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 var isSelectionInsideNode = isSelectionInNode(start, view); if (!isSelectionInsideNode || isNonEditableBlock(start, view)) { api.core.actions.execute(createNewLine(start)); } var codeBlock = view.state.schema.nodes.codeBlock; var selection = view.state.selection; var 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 var currentSelection = view.state.selection; if (isInTextSelection(view) && currentSelection.from !== currentSelection.to) { var currentParagraphNode = findParentNode(function (node) { return TEXT_PARENT_TYPES.includes(node.type.name); })(currentSelection); if (currentParagraphNode) { var 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(function (_ref6) { var tr = _ref6.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(function (_ref7) { var tr = _ref7.tr; createNewLine(view.state.selection.from)({ tr: 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 var lastInlinePosition = TextSelection.near(view.state.selection.$to, -1); lastInlinePosition && api.core.actions.execute(function (_ref8) { var tr = _ref8.tr; if (!(lastInlinePosition instanceof TextSelection)) { // this will create a new line after the node createNewLine(lastInlinePosition.from)({ tr: 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 || _api$quickInsert.actions.openTypeAhead('blockControl', true); };