UNPKG

@atlaskit/editor-plugin-selection-extension

Version:

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

205 lines (201 loc) 10.6 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getFragmentInfoFromSelectionNew = exports.getFragmentInfoFromSelection = void 0; exports.getSelectionAdfInfo = getSelectionAdfInfo; exports.getSelectionAdfInfoNew = getSelectionAdfInfoNew; exports.getSelectionTextInfoNew = exports.getSelectionTextInfo = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _monitoring = require("@atlaskit/editor-common/monitoring"); var _editorJsonTransformer = require("@atlaskit/editor-json-transformer"); var _model = require("@atlaskit/editor-prosemirror/model"); var _state = require("@atlaskit/editor-prosemirror/state"); var _transform = require("@atlaskit/editor-prosemirror/transform"); var _editorSharedStyles = require("@atlaskit/editor-shared-styles"); var _editorTables = require("@atlaskit/editor-tables"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _getBoundingBoxFromSelection = require("../../ui/getBoundingBoxFromSelection"); var _selectionHelpers = require("./selection-helpers"); 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) { (0, _defineProperty2.default)(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; } var getSelectedRect = function getSelectedRect(selection) { var $anchorCell = selection.$anchorCell, $headCell = selection.$headCell; var table = $anchorCell.node(-1); var map = _editorTables.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 */ var getSelectionTextInfo = exports.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 = (0, _getBoundingBoxFromSelection.getBoundingBoxFromSelection)(view, from, to, { top: shouldOffsetToolbarHeight ? _editorSharedStyles.akEditorFullPageToolbarHeight : 0, bottom: shouldOffsetToolbarHeight ? _editorSharedStyles.akEditorFullPageToolbarHeight : 0 }); return { text: text, from: from, to: to, coords: coords }; }; var getSelectionTextInfoNew = exports.getSelectionTextInfoNew = function getSelectionTextInfoNew(selection, view, api) { var _api$userPreferences2, _api$selectionToolbar, _api$editorViewMode2; var toolbarDocking = (0, _platformFeatureFlags.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 = (0, _getBoundingBoxFromSelection.getBoundingBoxFromSelection)(view, from, to, { top: shouldOffsetToolbarHeight ? _editorSharedStyles.akEditorFullPageToolbarHeight : 0, bottom: shouldOffsetToolbarHeight ? _editorSharedStyles.akEditorFullPageToolbarHeight : 0 }); return { text: text, from: from, to: to, coords: coords }; }; /** * @private * @deprecated use getFragmentInfoFromSelectionNew instead */ var getFragmentInfoFromSelection = exports.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.Transform(doc); newDoc = transform.replaceRange(0, 2, slice).doc; } catch (error) { newDoc = schema.nodes.doc.createChecked({}, _model.Fragment.empty); (0, _monitoring.logException)(error, { location: 'editor-plugin-selection-extension' }); } var serializer = new _editorJsonTransformer.JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(newDoc); return { selectedNodeAdf: selectedNodeAdf }; }; var getFragmentInfoFromSelectionNew = exports.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.Transform(doc); newDoc = transform.replaceRange(0, 2, slice).doc; } catch (error) { newDoc = schema.nodes.doc.createChecked({}, _model.Fragment.empty); (0, _monitoring.logException)(error, { location: 'editor-plugin-selection-extension' }); } var serializer = new _editorJsonTransformer.JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(newDoc); return { selectedNodeAdf: selectedNodeAdf }; }; /** * @private * @deprecated use getSelectionAdfInfoNew instead */ 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 _state.TextSelection) { if ((0, _platformFeatureFlags.fg)('platform_editor_selection_extension_improvement')) { // New implementation: unified handler for all text selections selectionInfo = (0, _selectionHelpers.getSelectionInfo)(selection, state.schema); } else { var $from = selection.$from, $to = selection.$to; if ($from.parent === $to.parent) { selectionInfo = (0, _selectionHelpers.getSelectionInfoFromSameNode)(selection); } } } else if (selection instanceof _editorTables.CellSelection) { selectionInfo = getSelectionInfoFromCellSelection(selection); } var serializer = new _editorJsonTransformer.JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(selectionInfo.selectedNode); return _objectSpread(_objectSpread({}, selectionInfo), {}, { selectedNodeAdf: selectedNodeAdf }); } 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 _state.TextSelection) { if ((0, _platformFeatureFlags.fg)('platform_editor_selection_extension_improvement')) { selectionInfo = (0, _selectionHelpers.getSelectionInfo)(selection, schema); } else { var $from = selection.$from, $to = selection.$to; if ($from.parent === $to.parent && $from.depth > 0) { selectionInfo = (0, _selectionHelpers.getSelectionInfoFromSameNode)(selection); } } } else if (selection instanceof _editorTables.CellSelection) { selectionInfo = getSelectionInfoFromCellSelection(selection); } else if (selection instanceof _state.NodeSelection) { selectionInfo = { selectedNode: selection.node, nodePos: selection.from }; } var serializer = new _editorJsonTransformer.JSONTransformer(); var selectedNodeAdf = serializer.encodeNode(selectionInfo.selectedNode); return _objectSpread(_objectSpread({}, selectionInfo), {}, { selectedNodeAdf: selectedNodeAdf }); }