UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

284 lines (278 loc) 12.1 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; } // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead import uuid from 'uuid/v4'; import { SetAttrsStep } from '@atlaskit/adf-schema/steps'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD, MODE, PLATFORMS } from '@atlaskit/editor-common/analytics'; import { expandedState } from '@atlaskit/editor-common/expand'; import { GapCursorSelection, Side } from '@atlaskit/editor-common/selection'; import { expandClassNames } from '@atlaskit/editor-common/styles'; import { findExpand } from '@atlaskit/editor-common/transforms'; import { createWrapSelectionTransaction } from '@atlaskit/editor-common/utils'; import { Selection, TextSelection } from '@atlaskit/editor-prosemirror/state'; import { findParentNodeOfType, safeInsert } from '@atlaskit/editor-prosemirror/utils'; import { findTable } from '@atlaskit/editor-tables/utils'; import { fg } from '@atlaskit/platform-feature-flags'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { isNestedInExpand } from '../utils'; // Creates either an expand or a nestedExpand node based on the current selection export var createExpandNode = function createExpandNode(state) { var setExpandedState = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var addLocalId = arguments.length > 2 ? arguments[2] : undefined; var _state$schema$nodes = state.schema.nodes, expand = _state$schema$nodes.expand, nestedExpand = _state$schema$nodes.nestedExpand, paragraph = _state$schema$nodes.paragraph; var isSelectionInTable = !!findTable(state.selection); var isSelectionInExpand = isNestedInExpand(state); var expandType = isSelectionInTable || isSelectionInExpand ? nestedExpand : expand; var expandNode = fg('platform_editor_adf_with_localid') ? expandType.createAndFill(addLocalId ? { // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead localId: uuid() } : {}, // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead paragraph.createAndFill(addLocalId ? { localId: uuid() } : {})) : expandType.createAndFill({}); if (setExpandedState) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expandedState.set(expandNode, true); } return expandNode; }; /** * When cleaning up platform_editor_adf_with_localid we can reuse this function * in insertExpandWithInputMethod. */ export var wrapSelectionAndSetExpandedState = function wrapSelectionAndSetExpandedState(state, node) { var tr = createWrapSelectionTransaction({ state: state, type: node.type, nodeAttributes: node.attrs }); var wrapperNode = findParentNodeOfType(node.type)(tr.selection); if (wrapperNode) { expandedState.set(wrapperNode.node, true); } return tr; }; export var insertExpandWithInputMethod = function insertExpandWithInputMethod(api) { return function (inputMethod) { return function (state, dispatch) { var expandNode = createExpandNode(state, false, !!(api !== null && api !== void 0 && api.localId)); if (!expandNode) { return false; } var tr; if (state.selection.empty) { tr = safeInsert(expandNode)(state.tr).scrollIntoView(); expandedState.set(expandNode, true); } else { tr = createWrapSelectionTransaction(_objectSpread({ state: state, type: expandNode.type }, fg('platform_editor_adf_with_localid') && { nodeAttributes: expandNode.attrs })); var wrapperNode = findParentNodeOfType(expandNode.type)(tr.selection); if (wrapperNode) { expandedState.set(wrapperNode.node, true); } } var payload = { action: ACTION.INSERTED, actionSubject: ACTION_SUBJECT.DOCUMENT, actionSubjectId: (expandNode === null || expandNode === void 0 ? void 0 : expandNode.type) === state.schema.nodes.expand ? ACTION_SUBJECT_ID.EXPAND : ACTION_SUBJECT_ID.NESTED_EXPAND, attributes: { inputMethod: inputMethod }, eventType: EVENT_TYPE.TRACK }; if (dispatch) { var _api$analytics; api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || (_api$analytics = _api$analytics.actions) === null || _api$analytics === void 0 || _api$analytics.attachAnalyticsEvent(payload)(tr); dispatch(tr); } return true; }; }; }; export var insertExpand = function insertExpand(api) { return function (state, dispatch) { return insertExpandWithInputMethod(api)(INPUT_METHOD.INSERT_MENU)(state, dispatch); }; }; export var deleteExpand = function deleteExpand(editorAnalyticsAPI) { return function (state, dispatch) { var expandNode = findExpand(state); if (!expandNode) { return false; } return deleteExpandAtPos(editorAnalyticsAPI)(expandNode.pos, expandNode.node)(state, dispatch); }; }; export var deleteExpandAtPos = function deleteExpandAtPos(editorAnalyticsAPI) { return function (expandNodePos, expandNode) { return function (state, dispatch) { if (!expandNode || isNaN(expandNodePos)) { return false; } var payload = { action: ACTION.DELETED, actionSubject: expandNode.type === state.schema.nodes.expand ? ACTION_SUBJECT.EXPAND : ACTION_SUBJECT.NESTED_EXPAND, attributes: { inputMethod: INPUT_METHOD.FLOATING_TB }, eventType: EVENT_TYPE.TRACK }; if (expandNode && dispatch) { var tr = state.tr; tr.delete(expandNodePos, expandNodePos + expandNode.nodeSize); editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent(payload)(tr); if (expandNode.type === state.schema.nodes.nestedExpand) { var resolvedPos = tr.doc.resolve(expandNodePos + 1); if (resolvedPos) { tr.setSelection(Selection.near(resolvedPos, -1)); } } dispatch(tr); } return true; }; }; }; // Used to clear any node or cell selection when expand title is focused export var setSelectionInsideExpand = function setSelectionInsideExpand(expandPos) { return function (_state, dispatch, editorView) { if (editorView) { if (!editorView.hasFocus()) { editorView.focus(); } var sel = Selection.findFrom(editorView.state.doc.resolve(expandPos), 1, true); if (sel && dispatch) { dispatch(editorView.state.tr.setSelection(sel)); } return true; } return false; }; }; export var toggleExpandExpanded = function toggleExpandExpanded(_ref) { var editorAnalyticsAPI = _ref.editorAnalyticsAPI, pos = _ref.pos, node = _ref.node; return function (state, dispatch) { if (node && dispatch) { var _expandedState$get; var tr = state.tr; var expanded = (_expandedState$get = expandedState.get(node)) !== null && _expandedState$get !== void 0 ? _expandedState$get : false; var isExpandedNext = !expanded; expandedState.set(node, isExpandedNext); // If we're going to collapse the expand and our cursor is currently inside // Move to a right gap cursor, if the toolbar is interacted (or an API), // it will insert below rather than inside (which will be invisible). if (isExpandedNext === true) { tr.setSelection(new GapCursorSelection(tr.doc.resolve(pos + node.nodeSize), Side.RIGHT)); } // log when people open/close expands // TODO: ED-8523 - make platform/mode global attributes? var payload = { action: ACTION.TOGGLE_EXPAND, actionSubject: node.type === state.schema.nodes.expand ? ACTION_SUBJECT.EXPAND : ACTION_SUBJECT.NESTED_EXPAND, attributes: { platform: PLATFORMS.WEB, mode: MODE.EDITOR, expanded: isExpandedNext }, eventType: EVENT_TYPE.TRACK }; tr.setMeta('scrollIntoView', false); editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent(payload)(tr); dispatch(tr); } return true; }; }; export var updateExpandTitle = function updateExpandTitle(_ref2) { var title = _ref2.title, nodeType = _ref2.nodeType, pos = _ref2.pos; return function (state, dispatch) { var node = state.doc.nodeAt(pos); if (node && node.type === nodeType && dispatch) { var tr = state.tr; tr.step(new SetAttrsStep(pos, _objectSpread(_objectSpread({}, node.attrs), {}, { title: title }))); dispatch(tr); } return true; }; }; export var focusTitle = function focusTitle(pos) { return function (state, dispatch, editorView) { if (editorView) { var dom = editorView.domAtPos(pos); var expandWrapper = dom.node.parentElement; if (expandWrapper) { setSelectionInsideExpand(pos)(state, dispatch, editorView); var input = expandWrapper.querySelector('input'); if (input) { input.focus(); return true; } } } return false; }; }; export var focusIcon = function focusIcon(expand) { return function (state, dispatch, editorView) { if (!(expand instanceof HTMLElement)) { return false; } // TODO: ED-29205 - During platform_editor_vc90_transition_expand_icon cleanup, rename `iconContainer` to `iconButton`. var iconContainer = expValEquals('platform_editor_vc90_transition_expand_icon', 'isEnabled', true) ? expand.querySelector(".".concat(expandClassNames.iconButton)) : expand.querySelector(".".concat(expandClassNames.iconContainer)); if (iconContainer && iconContainer.focus) { var tr = state.tr; var pos = state.selection.from; tr.setSelection(new TextSelection(tr.doc.resolve(pos))); if (dispatch) { dispatch(tr); } editorView === null || editorView === void 0 || editorView.dom.blur(); iconContainer.focus(); return true; } return false; }; }; export var toggleExpandWithMatch = function toggleExpandWithMatch(selection) { return function (_ref3) { var tr = _ref3.tr; var _tr$doc$type$schema$n = tr.doc.type.schema.nodes, expand = _tr$doc$type$schema$n.expand, nestedExpand = _tr$doc$type$schema$n.nestedExpand; // if match is inside a nested expand, open the nested expand var nestedExpandNode = findParentNodeOfType(nestedExpand)(selection); if (nestedExpandNode) { var _expandedState$get2; var expanded = (_expandedState$get2 = expandedState.get(nestedExpandNode.node)) !== null && _expandedState$get2 !== void 0 ? _expandedState$get2 : false; if (!expanded) { expandedState.set(nestedExpandNode.node, true); } } // if match is (also) inside an expand, open the expand var expandNode = findParentNodeOfType(expand)(selection); if (expandNode) { var _expandedState$get3; var _expanded = (_expandedState$get3 = expandedState.get(expandNode.node)) !== null && _expandedState$get3 !== void 0 ? _expandedState$get3 : false; if (!_expanded) { expandedState.set(expandNode.node, true); } } return tr; }; };