UNPKG

@atlaskit/editor-plugin-selection-extension

Version:

editor-plugin-selection-extension plugin for @atlaskit/editor-core

195 lines (192 loc) 9.56 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 { logException } from '@atlaskit/editor-common/monitoring'; import { JSONTransformer } from '@atlaskit/editor-json-transformer'; import { Fragment } from '@atlaskit/editor-prosemirror/model'; import { NodeSelection, TextSelection } from '@atlaskit/editor-prosemirror/state'; import { Transform } from '@atlaskit/editor-prosemirror/transform'; import { akEditorFullPageToolbarHeight } from '@atlaskit/editor-shared-styles'; import { CellSelection, TableMap } from '@atlaskit/editor-tables'; import { fg } from '@atlaskit/platform-feature-flags'; import { getBoundingBoxFromSelection } from '../../ui/getBoundingBoxFromSelection'; import { getSelectionInfo, getSelectionInfoFromSameNode } from './selection-helpers'; var getSelectedRect = function getSelectedRect(selection) { var $anchorCell = selection.$anchorCell, $headCell = selection.$headCell; var table = $anchorCell.node(-1); var map = TableMap.get(table); var start = $anchorCell.start(-1); var rect = map.rectBetween($anchorCell.pos - start, $headCell.pos - start); return rect; }; var getSelectionInfoFromCellSelection = function getSelectionInfoFromCellSelection(selection) { var selectedNode = selection.$anchorCell.node(-1); var nodePos = selection.$anchorCell.before(-1); var selectionRanges = []; var rect = getSelectedRect(selection); for (var row = rect.top; row < rect.bottom; row++) { selectionRanges.push({ start: { pointer: "/content/".concat(row, "/content/").concat(rect.left) }, end: { pointer: "/content/".concat(row, "/content/").concat(rect.right - 1) } }); } return { selectedNode: selectedNode, selectionRanges: selectionRanges, nodePos: nodePos }; }; /** * @private * @deprecated use getSelectionTextInfoNew instead */ export var getSelectionTextInfo = function getSelectionTextInfo(view, api) { var _api$userPreferences, _api$editorViewMode; var currentSelection = view.state.selection; var toolbarDocking = api === null || api === void 0 || (_api$userPreferences = api.userPreferences) === null || _api$userPreferences === void 0 || (_api$userPreferences = _api$userPreferences.sharedState.currentState()) === null || _api$userPreferences === void 0 || (_api$userPreferences = _api$userPreferences.preferences) === null || _api$userPreferences === void 0 ? void 0 : _api$userPreferences.toolbarDockingPosition; var isEditMode = Boolean((api === null || api === void 0 || (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 || (_api$editorViewMode = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.mode) === 'edit'); var shouldOffsetToolbarHeight = toolbarDocking === 'top' && isEditMode; var from = currentSelection.from, to = currentSelection.to; var text = view.state.doc.textBetween(from, to, '\n'); var coords = getBoundingBoxFromSelection(view, from, to, { top: shouldOffsetToolbarHeight ? akEditorFullPageToolbarHeight : 0, bottom: shouldOffsetToolbarHeight ? akEditorFullPageToolbarHeight : 0 }); return { text: text, from: from, to: to, coords: coords }; }; export var getSelectionTextInfoNew = function getSelectionTextInfoNew(selection, view, api) { var _api$userPreferences2, _api$selectionToolbar, _api$editorViewMode2; var toolbarDocking = fg('platform_editor_use_preferences_plugin') ? api === null || api === void 0 || (_api$userPreferences2 = api.userPreferences) === null || _api$userPreferences2 === void 0 || (_api$userPreferences2 = _api$userPreferences2.sharedState.currentState()) === null || _api$userPreferences2 === void 0 || (_api$userPreferences2 = _api$userPreferences2.preferences) === null || _api$userPreferences2 === void 0 ? void 0 : _api$userPreferences2.toolbarDockingPosition : api === null || api === void 0 || (_api$selectionToolbar = api.selectionToolbar) === null || _api$selectionToolbar === void 0 || (_api$selectionToolbar = _api$selectionToolbar.sharedState) === null || _api$selectionToolbar === void 0 || (_api$selectionToolbar = _api$selectionToolbar.currentState()) === null || _api$selectionToolbar === void 0 ? void 0 : _api$selectionToolbar.toolbarDocking; var isEditMode = Boolean((api === null || api === void 0 || (_api$editorViewMode2 = api.editorViewMode) === null || _api$editorViewMode2 === void 0 || (_api$editorViewMode2 = _api$editorViewMode2.sharedState.currentState()) === null || _api$editorViewMode2 === void 0 ? void 0 : _api$editorViewMode2.mode) === 'edit'); var shouldOffsetToolbarHeight = toolbarDocking === 'top' && isEditMode; var from = selection.from, to = selection.to; var text = view.state.doc.textBetween(from, to, '\n'); var coords = getBoundingBoxFromSelection(view, from, to, { top: shouldOffsetToolbarHeight ? akEditorFullPageToolbarHeight : 0, bottom: shouldOffsetToolbarHeight ? akEditorFullPageToolbarHeight : 0 }); return { text: text, from: from, to: to, coords: coords }; }; /** * @private * @deprecated use getFragmentInfoFromSelectionNew instead */ export var getFragmentInfoFromSelection = function getFragmentInfoFromSelection(state) { var schema = state.schema, selection = state.selection; var slice = selection.content(); var newDoc; try { var _schema = state.schema; var doc = _schema.node('doc', null, [_schema.node('paragraph', null, [])]); var transform = new Transform(doc); newDoc = transform.replaceRange(0, 2, slice).doc; } catch (error) { newDoc = schema.nodes.doc.createChecked({}, Fragment.empty); logException(error, { location: 'editor-plugin-selection-extension' }); } var serializer = new JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(newDoc); return { selectedNodeAdf: selectedNodeAdf }; }; export var getFragmentInfoFromSelectionNew = function getFragmentInfoFromSelectionNew(selection) { var schema = selection.$from.doc.type.schema; var slice = selection.content(); var newDoc; try { var doc = schema.node('doc', null, [schema.node('paragraph', null, [])]); var transform = new Transform(doc); newDoc = transform.replaceRange(0, 2, slice).doc; } catch (error) { newDoc = schema.nodes.doc.createChecked({}, Fragment.empty); logException(error, { location: 'editor-plugin-selection-extension' }); } var serializer = new JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(newDoc); return { selectedNodeAdf: selectedNodeAdf }; }; /** * @private * @deprecated use getSelectionAdfInfoNew instead */ export function getSelectionAdfInfo(state) { var selection = state.selection; var selectionInfo = { selectedNode: selection.$from.node(), nodePos: selection.$from.depth > 0 ? selection.$from.before() : selection.from }; if (selection instanceof TextSelection) { if (fg('platform_editor_selection_extension_improvement')) { // New implementation: unified handler for all text selections selectionInfo = getSelectionInfo(selection, state.schema); } else { var $from = selection.$from, $to = selection.$to; if ($from.parent === $to.parent) { selectionInfo = getSelectionInfoFromSameNode(selection); } } } else if (selection instanceof CellSelection) { selectionInfo = getSelectionInfoFromCellSelection(selection); } var serializer = new JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(selectionInfo.selectedNode); return _objectSpread(_objectSpread({}, selectionInfo), {}, { selectedNodeAdf: selectedNodeAdf }); } export function getSelectionAdfInfoNew(selection) { var schema = selection.$from.doc.type.schema; var selectionInfo = { selectedNode: selection.$from.node(), nodePos: selection.$from.depth > 0 ? selection.$from.before() : selection.from }; if (selection instanceof TextSelection) { if (fg('platform_editor_selection_extension_improvement')) { selectionInfo = getSelectionInfo(selection, schema); } else { var $from = selection.$from, $to = selection.$to; if ($from.parent === $to.parent && $from.depth > 0) { selectionInfo = getSelectionInfoFromSameNode(selection); } } } else if (selection instanceof CellSelection) { selectionInfo = getSelectionInfoFromCellSelection(selection); } else if (selection instanceof NodeSelection) { selectionInfo = { selectedNode: selection.node, nodePos: selection.from }; } var serializer = new JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(selectionInfo.selectedNode); return _objectSpread(_objectSpread({}, selectionInfo), {}, { selectedNodeAdf: selectedNodeAdf }); }