@atlaskit/editor-plugin-selection-marker 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE.md +13 -0
  3. package/README.md +39 -0
  4. package/dist/cjs/index.js +12 -0
  5. package/dist/cjs/plugin.js +43 -0
  6. package/dist/cjs/pm-plugins/main.js +57 -0
  7. package/dist/cjs/types.js +5 -0
  8. package/dist/cjs/ui/global-styles.js +22 -0
  9. package/dist/cjs/ui/selection-decoration.js +14 -0
  10. package/dist/cjs/ui/widget-decoration.js +68 -0
  11. package/dist/es2019/index.js +1 -0
  12. package/dist/es2019/plugin.js +33 -0
  13. package/dist/es2019/pm-plugins/main.js +51 -0
  14. package/dist/es2019/types.js +1 -0
  15. package/dist/es2019/ui/global-styles.js +16 -0
  16. package/dist/es2019/ui/selection-decoration.js +11 -0
  17. package/dist/es2019/ui/widget-decoration.js +62 -0
  18. package/dist/esm/index.js +1 -0
  19. package/dist/esm/plugin.js +34 -0
  20. package/dist/esm/pm-plugins/main.js +48 -0
  21. package/dist/esm/types.js +1 -0
  22. package/dist/esm/ui/global-styles.js +16 -0
  23. package/dist/esm/ui/selection-decoration.js +8 -0
  24. package/dist/esm/ui/widget-decoration.js +61 -0
  25. package/dist/types/index.d.ts +2 -0
  26. package/dist/types/plugin.d.ts +2 -0
  27. package/dist/types/pm-plugins/main.d.ts +17 -0
  28. package/dist/types/types.d.ts +6 -0
  29. package/dist/types/ui/global-styles.d.ts +3 -0
  30. package/dist/types/ui/selection-decoration.d.ts +3 -0
  31. package/dist/types/ui/widget-decoration.d.ts +6 -0
  32. package/dist/types-ts4.5/index.d.ts +2 -0
  33. package/dist/types-ts4.5/plugin.d.ts +2 -0
  34. package/dist/types-ts4.5/pm-plugins/main.d.ts +17 -0
  35. package/dist/types-ts4.5/types.d.ts +9 -0
  36. package/dist/types-ts4.5/ui/global-styles.d.ts +3 -0
  37. package/dist/types-ts4.5/ui/selection-decoration.d.ts +3 -0
  38. package/dist/types-ts4.5/ui/widget-decoration.d.ts +6 -0
  39. package/package.json +94 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ # @atlaskit/editor-plugin-selection-marker
package/LICENSE.md ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2023 Atlassian Pty Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Editor plugin selection marker
2
+
3
+ Selection marker plugin for @atlaskit/editor-core
4
+
5
+ **Note:** This component is designed for internal Atlassian development.
6
+ External contributors will be able to use this component but will not be able to submit issues.
7
+
8
+ ## Install
9
+
10
+ ---
11
+
12
+ - **Install** - _yarn add @atlaskit/editor-plugin-selection-marker_
13
+ - **npm** - [@atlaskit/editor-plugin-selection-marker](https://www.npmjs.com/package/@atlaskit/editor-plugin-selection-marker)
14
+ - **Source** - [Bitbucket](https://bitbucket.org/atlassian/atlassian-frontend/src/master/packages/editor/editor-plugin-selection-marker)
15
+ - **Bundle** - [unpkg.com](https://unpkg.com/@atlaskit/editor-plugin-selection-marker/dist/)
16
+
17
+ ## Usage
18
+
19
+ ---
20
+
21
+ **Internal use only**
22
+
23
+ @atlaskit/editor-plugin-selection-marker is intended for internal use by the @atlaskit/editor-core and as a plugin dependency of the Editor within your product.
24
+
25
+ Direct use of this component is not supported.
26
+
27
+ Please see [Atlaskit - Editor plugin selection marker](https://atlaskit.atlassian.com/packages/editor/editor-plugin-selection-marker) for documentation and examples for this package.
28
+
29
+ ## Support
30
+
31
+ ---
32
+
33
+ For internal Atlassian, visit the slack channel [#help-editor](https://atlassian.slack.com/archives/CFG3PSQ9E) for support or visit [go/editor-help](https://go/editor-help) to submit a bug.
34
+
35
+ ## License
36
+
37
+ ---
38
+
39
+ Please see [Atlassian Frontend - License](https://hello.atlassian.net/wiki/spaces/AF/pages/2589099144/Documentation#License) for more licensing information.
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "selectionMarkerPlugin", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _plugin.selectionMarkerPlugin;
10
+ }
11
+ });
12
+ var _plugin = require("./plugin");
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ var _typeof = require("@babel/runtime/helpers/typeof");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.selectionMarkerPlugin = void 0;
8
+ var _react = _interopRequireWildcard(require("react"));
9
+ var _hooks = require("@atlaskit/editor-common/hooks");
10
+ var _main = require("./pm-plugins/main");
11
+ var _globalStyles = require("./ui/global-styles");
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
+ var selectionMarkerPlugin = exports.selectionMarkerPlugin = function selectionMarkerPlugin(_ref) {
15
+ var api = _ref.api;
16
+ return {
17
+ name: 'selectionMarker',
18
+ pmPlugins: function pmPlugins() {
19
+ return [{
20
+ name: 'selectionMarkerPmPlugin',
21
+ plugin: function plugin() {
22
+ return (0, _main.createPlugin)(api);
23
+ }
24
+ }];
25
+ },
26
+ usePluginHook: function usePluginHook(_ref2) {
27
+ var editorView = _ref2.editorView;
28
+ var _useSharedPluginState = (0, _hooks.useSharedPluginState)(api, ['focus', 'typeAhead']),
29
+ focusState = _useSharedPluginState.focusState,
30
+ typeAheadState = _useSharedPluginState.typeAheadState;
31
+ (0, _react.useEffect)(function () {
32
+ var _ref3, _typeAheadState$isOpe;
33
+ var shouldHide = (_ref3 = (focusState === null || focusState === void 0 ? void 0 : focusState.hasFocus) || ((_typeAheadState$isOpe = typeAheadState === null || typeAheadState === void 0 ? void 0 : typeAheadState.isOpen) !== null && _typeAheadState$isOpe !== void 0 ? _typeAheadState$isOpe : false)) !== null && _ref3 !== void 0 ? _ref3 : true;
34
+ requestAnimationFrame(function () {
35
+ return (0, _main.dispatchShouldHideDecorations)(editorView, shouldHide);
36
+ });
37
+ }, [editorView, focusState, typeAheadState]);
38
+ },
39
+ contentComponent: function contentComponent() {
40
+ return /*#__PURE__*/_react.default.createElement(_globalStyles.GlobalStylesWrapper, null);
41
+ }
42
+ };
43
+ };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.createPlugin = void 0;
8
+ exports.dispatchShouldHideDecorations = dispatchShouldHideDecorations;
9
+ exports.key = void 0;
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
+ var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
12
+ var _state = require("@atlaskit/editor-prosemirror/state");
13
+ var _view = require("@atlaskit/editor-prosemirror/view");
14
+ var _selectionDecoration = require("../ui/selection-decoration");
15
+ var _widgetDecoration = require("../ui/widget-decoration");
16
+ var key = exports.key = new _state.PluginKey('selectionMarker');
17
+ var createPlugin = exports.createPlugin = function createPlugin(api) {
18
+ return new _safePlugin.SafePlugin({
19
+ key: key,
20
+ state: {
21
+ init: function init() {
22
+ return {
23
+ decorations: _view.DecorationSet.empty,
24
+ shouldHideDecorations: true
25
+ };
26
+ },
27
+ apply: function apply(tr, currentState) {
28
+ var _tr$getMeta$shouldHid, _tr$getMeta;
29
+ var shouldHideDecorations = (_tr$getMeta$shouldHid = (_tr$getMeta = tr.getMeta(key)) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.shouldHideDecorations) !== null && _tr$getMeta$shouldHid !== void 0 ? _tr$getMeta$shouldHid : currentState.shouldHideDecorations;
30
+ var selection = tr.selection;
31
+ if (shouldHideDecorations) {
32
+ return {
33
+ decorations: _view.DecorationSet.empty,
34
+ shouldHideDecorations: shouldHideDecorations
35
+ };
36
+ }
37
+ return {
38
+ decorations: _view.DecorationSet.create(tr.doc, [].concat((0, _toConsumableArray2.default)((0, _widgetDecoration.createWidgetDecoration)(selection.$anchor, 'anchor', selection)), [(0, _selectionDecoration.selectionDecoration)(selection)], (0, _toConsumableArray2.default)((0, _widgetDecoration.createWidgetDecoration)(selection.$head, 'head', selection)))),
39
+ shouldHideDecorations: shouldHideDecorations
40
+ };
41
+ }
42
+ },
43
+ props: {
44
+ decorations: function decorations(state) {
45
+ var _key$getState;
46
+ return (_key$getState = key.getState(state)) === null || _key$getState === void 0 ? void 0 : _key$getState.decorations;
47
+ }
48
+ }
49
+ });
50
+ };
51
+ function dispatchShouldHideDecorations(editorView, shouldHideDecorations) {
52
+ var dispatch = editorView.dispatch,
53
+ state = editorView.state;
54
+ dispatch(state.tr.setMeta(key, {
55
+ shouldHideDecorations: shouldHideDecorations
56
+ }));
57
+ }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.GlobalStylesWrapper = void 0;
7
+ var _react = require("@emotion/react");
8
+ /** @jsx jsx */
9
+
10
+ // Unset the selection background color as we are using our own
11
+ // Otherwise we might have a mix of grey + our selection marker depending on the state.
12
+ var globalStyles = (0, _react.css)({
13
+ // eslint-disable-next-line @atlaskit/design-system/no-nested-styles
14
+ '.ProseMirror:not(:focus) ::selection': {
15
+ background: 'unset'
16
+ }
17
+ });
18
+ var GlobalStylesWrapper = exports.GlobalStylesWrapper = function GlobalStylesWrapper() {
19
+ return (0, _react.jsx)(_react.Global, {
20
+ styles: globalStyles
21
+ });
22
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.selectionDecoration = void 0;
7
+ var _view = require("@atlaskit/editor-prosemirror/view");
8
+ var decorationStyle = "\n background-color: ".concat("var(--ds-background-accent-blue-subtlest, #E9F2FF)", ";\n border-bottom: 2px solid ", "var(--ds-background-accent-blue-subtler, #cce0ff)", ";\n ");
9
+ var selectionDecoration = exports.selectionDecoration = function selectionDecoration(selection) {
10
+ return _view.Decoration.inline(selection.from, selection.to, {
11
+ style: decorationStyle,
12
+ 'data-testid': 'selection-marker-selection'
13
+ });
14
+ };
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.createWidgetDecoration = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _reactDom = _interopRequireDefault(require("react-dom"));
10
+ var _state = require("@atlaskit/editor-prosemirror/state");
11
+ var _view = require("@atlaskit/editor-prosemirror/view");
12
+ var _primitives = require("@atlaskit/primitives");
13
+ // Copied from: platform/packages/editor/editor-plugin-ai/src/ui/modal/styles.tsx
14
+ var selectionMarkerStyles = (0, _primitives.xcss)({
15
+ content: "''",
16
+ position: 'absolute',
17
+ backgroundImage: "url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMyIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDMgMjAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMSAxSDBMMSAxLjg1NzE0VjE4LjE0MzNMMCAxOS4wMDA0SDNMMiAxOC4xNDMzVjEuODU3MTRMMyAxSDJIMVoiIGZpbGw9IiM1NzlERkYiLz4KPHJlY3QgeT0iMTkiIHdpZHRoPSIzIiBoZWlnaHQ9IjEiIGZpbGw9IiM1NzlERkYiLz4KPHJlY3Qgd2lkdGg9IjMiIGhlaWdodD0iMSIgZmlsbD0iIzU3OURGRiIvPgo8L3N2Zz4K')",
18
+ top: 'space.0',
19
+ bottom: "var(--ds-space-negative-025, -2px)",
20
+ backgroundRepeat: 'no-repeat',
21
+ backgroundPositionX: 'center',
22
+ backgroundPositionY: 'center',
23
+ backgroundSize: 'contain',
24
+ aspectRatio: '3/20',
25
+ left: '0px',
26
+ marginLeft: '-0.1em',
27
+ right: '0px',
28
+ marginRight: '-0.1em',
29
+ pointerEvents: 'none'
30
+ });
31
+ var Widget = function Widget(_ref) {
32
+ var type = _ref.type;
33
+ return /*#__PURE__*/_react.default.createElement(_primitives.Box, {
34
+ as: 'span',
35
+ xcss: selectionMarkerStyles,
36
+ testId: "selection-marker-".concat(type, "-cursor"),
37
+ contentEditable: false
38
+ });
39
+ };
40
+ var toDOM = function toDOM(type) {
41
+ var element = document.createElement('span');
42
+ element.contentEditable = 'false';
43
+ element.setAttribute('style', "position: relative;");
44
+ _reactDom.default.render( /*#__PURE__*/_react.default.createElement(Widget, {
45
+ type: type
46
+ }), element);
47
+ return element;
48
+ };
49
+ var containsText = function containsText(resolvedPos) {
50
+ var nodeBefore = resolvedPos.nodeBefore,
51
+ nodeAfter = resolvedPos.nodeAfter;
52
+ return (nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.isText) || (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.isText);
53
+ };
54
+ var createWidgetDecoration = exports.createWidgetDecoration = function createWidgetDecoration(resolvedPos, type, selection) {
55
+ // We don't want the cursor to show if it's not text selection
56
+ // ie. if it's on media selection
57
+ if (!(selection instanceof _state.TextSelection) || containsText(resolvedPos) === false) {
58
+ return [];
59
+ }
60
+ return [_view.Decoration.widget(resolvedPos.pos, toDOM(type), {
61
+ side: -1,
62
+ key: "".concat(type, "WidgetDecoration"),
63
+ stopEvent: function stopEvent() {
64
+ return true;
65
+ },
66
+ ignoreSelection: true
67
+ })];
68
+ };
@@ -0,0 +1 @@
1
+ export { selectionMarkerPlugin } from './plugin';
@@ -0,0 +1,33 @@
1
+ import React, { useEffect } from 'react';
2
+ import { useSharedPluginState } from '@atlaskit/editor-common/hooks';
3
+ import { createPlugin, dispatchShouldHideDecorations } from './pm-plugins/main';
4
+ import { GlobalStylesWrapper } from './ui/global-styles';
5
+ export const selectionMarkerPlugin = ({
6
+ api
7
+ }) => {
8
+ return {
9
+ name: 'selectionMarker',
10
+ pmPlugins() {
11
+ return [{
12
+ name: 'selectionMarkerPmPlugin',
13
+ plugin: () => createPlugin(api)
14
+ }];
15
+ },
16
+ usePluginHook({
17
+ editorView
18
+ }) {
19
+ const {
20
+ focusState,
21
+ typeAheadState
22
+ } = useSharedPluginState(api, ['focus', 'typeAhead']);
23
+ useEffect(() => {
24
+ var _ref, _typeAheadState$isOpe;
25
+ const shouldHide = (_ref = (focusState === null || focusState === void 0 ? void 0 : focusState.hasFocus) || ((_typeAheadState$isOpe = typeAheadState === null || typeAheadState === void 0 ? void 0 : typeAheadState.isOpen) !== null && _typeAheadState$isOpe !== void 0 ? _typeAheadState$isOpe : false)) !== null && _ref !== void 0 ? _ref : true;
26
+ requestAnimationFrame(() => dispatchShouldHideDecorations(editorView, shouldHide));
27
+ }, [editorView, focusState, typeAheadState]);
28
+ },
29
+ contentComponent() {
30
+ return /*#__PURE__*/React.createElement(GlobalStylesWrapper, null);
31
+ }
32
+ };
33
+ };
@@ -0,0 +1,51 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
3
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
4
+ import { selectionDecoration } from '../ui/selection-decoration';
5
+ import { createWidgetDecoration } from '../ui/widget-decoration';
6
+ export const key = new PluginKey('selectionMarker');
7
+ export const createPlugin = api => {
8
+ return new SafePlugin({
9
+ key,
10
+ state: {
11
+ init() {
12
+ return {
13
+ decorations: DecorationSet.empty,
14
+ shouldHideDecorations: true
15
+ };
16
+ },
17
+ apply(tr, currentState) {
18
+ var _tr$getMeta$shouldHid, _tr$getMeta;
19
+ const shouldHideDecorations = (_tr$getMeta$shouldHid = (_tr$getMeta = tr.getMeta(key)) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.shouldHideDecorations) !== null && _tr$getMeta$shouldHid !== void 0 ? _tr$getMeta$shouldHid : currentState.shouldHideDecorations;
20
+ const {
21
+ selection
22
+ } = tr;
23
+ if (shouldHideDecorations) {
24
+ return {
25
+ decorations: DecorationSet.empty,
26
+ shouldHideDecorations
27
+ };
28
+ }
29
+ return {
30
+ decorations: DecorationSet.create(tr.doc, [...createWidgetDecoration(selection.$anchor, 'anchor', selection), selectionDecoration(selection), ...createWidgetDecoration(selection.$head, 'head', selection)]),
31
+ shouldHideDecorations
32
+ };
33
+ }
34
+ },
35
+ props: {
36
+ decorations: state => {
37
+ var _key$getState;
38
+ return (_key$getState = key.getState(state)) === null || _key$getState === void 0 ? void 0 : _key$getState.decorations;
39
+ }
40
+ }
41
+ });
42
+ };
43
+ export function dispatchShouldHideDecorations(editorView, shouldHideDecorations) {
44
+ const {
45
+ dispatch,
46
+ state
47
+ } = editorView;
48
+ dispatch(state.tr.setMeta(key, {
49
+ shouldHideDecorations
50
+ }));
51
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ /** @jsx jsx */
2
+ import { css, Global, jsx } from '@emotion/react';
3
+
4
+ // Unset the selection background color as we are using our own
5
+ // Otherwise we might have a mix of grey + our selection marker depending on the state.
6
+ const globalStyles = css({
7
+ // eslint-disable-next-line @atlaskit/design-system/no-nested-styles
8
+ '.ProseMirror:not(:focus) ::selection': {
9
+ background: 'unset'
10
+ }
11
+ });
12
+ export const GlobalStylesWrapper = () => {
13
+ return jsx(Global, {
14
+ styles: globalStyles
15
+ });
16
+ };
@@ -0,0 +1,11 @@
1
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
2
+ const decorationStyle = `
3
+ background-color: ${"var(--ds-background-accent-blue-subtlest, #E9F2FF)"};
4
+ border-bottom: 2px solid ${"var(--ds-background-accent-blue-subtler, #cce0ff)"};
5
+ `;
6
+ export const selectionDecoration = selection => {
7
+ return Decoration.inline(selection.from, selection.to, {
8
+ style: decorationStyle,
9
+ 'data-testid': 'selection-marker-selection'
10
+ });
11
+ };
@@ -0,0 +1,62 @@
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import { TextSelection } from '@atlaskit/editor-prosemirror/state';
4
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
5
+ import { Box, xcss } from '@atlaskit/primitives';
6
+ // Copied from: platform/packages/editor/editor-plugin-ai/src/ui/modal/styles.tsx
7
+ const selectionMarkerStyles = xcss({
8
+ content: "''",
9
+ position: 'absolute',
10
+ backgroundImage: "url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMyIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDMgMjAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMSAxSDBMMSAxLjg1NzE0VjE4LjE0MzNMMCAxOS4wMDA0SDNMMiAxOC4xNDMzVjEuODU3MTRMMyAxSDJIMVoiIGZpbGw9IiM1NzlERkYiLz4KPHJlY3QgeT0iMTkiIHdpZHRoPSIzIiBoZWlnaHQ9IjEiIGZpbGw9IiM1NzlERkYiLz4KPHJlY3Qgd2lkdGg9IjMiIGhlaWdodD0iMSIgZmlsbD0iIzU3OURGRiIvPgo8L3N2Zz4K')",
11
+ top: 'space.0',
12
+ bottom: "var(--ds-space-negative-025, -2px)",
13
+ backgroundRepeat: 'no-repeat',
14
+ backgroundPositionX: 'center',
15
+ backgroundPositionY: 'center',
16
+ backgroundSize: 'contain',
17
+ aspectRatio: '3/20',
18
+ left: '0px',
19
+ marginLeft: '-0.1em',
20
+ right: '0px',
21
+ marginRight: '-0.1em',
22
+ pointerEvents: 'none'
23
+ });
24
+ const Widget = ({
25
+ type
26
+ }) => {
27
+ return /*#__PURE__*/React.createElement(Box, {
28
+ as: 'span',
29
+ xcss: selectionMarkerStyles,
30
+ testId: `selection-marker-${type}-cursor`,
31
+ contentEditable: false
32
+ });
33
+ };
34
+ const toDOM = type => {
35
+ let element = document.createElement('span');
36
+ element.contentEditable = 'false';
37
+ element.setAttribute('style', `position: relative;`);
38
+ ReactDOM.render( /*#__PURE__*/React.createElement(Widget, {
39
+ type: type
40
+ }), element);
41
+ return element;
42
+ };
43
+ const containsText = resolvedPos => {
44
+ const {
45
+ nodeBefore,
46
+ nodeAfter
47
+ } = resolvedPos;
48
+ return (nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.isText) || (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.isText);
49
+ };
50
+ export const createWidgetDecoration = (resolvedPos, type, selection) => {
51
+ // We don't want the cursor to show if it's not text selection
52
+ // ie. if it's on media selection
53
+ if (!(selection instanceof TextSelection) || containsText(resolvedPos) === false) {
54
+ return [];
55
+ }
56
+ return [Decoration.widget(resolvedPos.pos, toDOM(type), {
57
+ side: -1,
58
+ key: `${type}WidgetDecoration`,
59
+ stopEvent: () => true,
60
+ ignoreSelection: true
61
+ })];
62
+ };
@@ -0,0 +1 @@
1
+ export { selectionMarkerPlugin } from './plugin';
@@ -0,0 +1,34 @@
1
+ import React, { useEffect } from 'react';
2
+ import { useSharedPluginState } from '@atlaskit/editor-common/hooks';
3
+ import { createPlugin, dispatchShouldHideDecorations } from './pm-plugins/main';
4
+ import { GlobalStylesWrapper } from './ui/global-styles';
5
+ export var selectionMarkerPlugin = function selectionMarkerPlugin(_ref) {
6
+ var api = _ref.api;
7
+ return {
8
+ name: 'selectionMarker',
9
+ pmPlugins: function pmPlugins() {
10
+ return [{
11
+ name: 'selectionMarkerPmPlugin',
12
+ plugin: function plugin() {
13
+ return createPlugin(api);
14
+ }
15
+ }];
16
+ },
17
+ usePluginHook: function usePluginHook(_ref2) {
18
+ var editorView = _ref2.editorView;
19
+ var _useSharedPluginState = useSharedPluginState(api, ['focus', 'typeAhead']),
20
+ focusState = _useSharedPluginState.focusState,
21
+ typeAheadState = _useSharedPluginState.typeAheadState;
22
+ useEffect(function () {
23
+ var _ref3, _typeAheadState$isOpe;
24
+ var shouldHide = (_ref3 = (focusState === null || focusState === void 0 ? void 0 : focusState.hasFocus) || ((_typeAheadState$isOpe = typeAheadState === null || typeAheadState === void 0 ? void 0 : typeAheadState.isOpen) !== null && _typeAheadState$isOpe !== void 0 ? _typeAheadState$isOpe : false)) !== null && _ref3 !== void 0 ? _ref3 : true;
25
+ requestAnimationFrame(function () {
26
+ return dispatchShouldHideDecorations(editorView, shouldHide);
27
+ });
28
+ }, [editorView, focusState, typeAheadState]);
29
+ },
30
+ contentComponent: function contentComponent() {
31
+ return /*#__PURE__*/React.createElement(GlobalStylesWrapper, null);
32
+ }
33
+ };
34
+ };
@@ -0,0 +1,48 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
3
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
4
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
5
+ import { selectionDecoration } from '../ui/selection-decoration';
6
+ import { createWidgetDecoration } from '../ui/widget-decoration';
7
+ export var key = new PluginKey('selectionMarker');
8
+ export var createPlugin = function createPlugin(api) {
9
+ return new SafePlugin({
10
+ key: key,
11
+ state: {
12
+ init: function init() {
13
+ return {
14
+ decorations: DecorationSet.empty,
15
+ shouldHideDecorations: true
16
+ };
17
+ },
18
+ apply: function apply(tr, currentState) {
19
+ var _tr$getMeta$shouldHid, _tr$getMeta;
20
+ var shouldHideDecorations = (_tr$getMeta$shouldHid = (_tr$getMeta = tr.getMeta(key)) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.shouldHideDecorations) !== null && _tr$getMeta$shouldHid !== void 0 ? _tr$getMeta$shouldHid : currentState.shouldHideDecorations;
21
+ var selection = tr.selection;
22
+ if (shouldHideDecorations) {
23
+ return {
24
+ decorations: DecorationSet.empty,
25
+ shouldHideDecorations: shouldHideDecorations
26
+ };
27
+ }
28
+ return {
29
+ decorations: DecorationSet.create(tr.doc, [].concat(_toConsumableArray(createWidgetDecoration(selection.$anchor, 'anchor', selection)), [selectionDecoration(selection)], _toConsumableArray(createWidgetDecoration(selection.$head, 'head', selection)))),
30
+ shouldHideDecorations: shouldHideDecorations
31
+ };
32
+ }
33
+ },
34
+ props: {
35
+ decorations: function decorations(state) {
36
+ var _key$getState;
37
+ return (_key$getState = key.getState(state)) === null || _key$getState === void 0 ? void 0 : _key$getState.decorations;
38
+ }
39
+ }
40
+ });
41
+ };
42
+ export function dispatchShouldHideDecorations(editorView, shouldHideDecorations) {
43
+ var dispatch = editorView.dispatch,
44
+ state = editorView.state;
45
+ dispatch(state.tr.setMeta(key, {
46
+ shouldHideDecorations: shouldHideDecorations
47
+ }));
48
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ /** @jsx jsx */
2
+ import { css, Global, jsx } from '@emotion/react';
3
+
4
+ // Unset the selection background color as we are using our own
5
+ // Otherwise we might have a mix of grey + our selection marker depending on the state.
6
+ var globalStyles = css({
7
+ // eslint-disable-next-line @atlaskit/design-system/no-nested-styles
8
+ '.ProseMirror:not(:focus) ::selection': {
9
+ background: 'unset'
10
+ }
11
+ });
12
+ export var GlobalStylesWrapper = function GlobalStylesWrapper() {
13
+ return jsx(Global, {
14
+ styles: globalStyles
15
+ });
16
+ };
@@ -0,0 +1,8 @@
1
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
2
+ var decorationStyle = "\n background-color: ".concat("var(--ds-background-accent-blue-subtlest, #E9F2FF)", ";\n border-bottom: 2px solid ", "var(--ds-background-accent-blue-subtler, #cce0ff)", ";\n ");
3
+ export var selectionDecoration = function selectionDecoration(selection) {
4
+ return Decoration.inline(selection.from, selection.to, {
5
+ style: decorationStyle,
6
+ 'data-testid': 'selection-marker-selection'
7
+ });
8
+ };
@@ -0,0 +1,61 @@
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom';
3
+ import { TextSelection } from '@atlaskit/editor-prosemirror/state';
4
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
5
+ import { Box, xcss } from '@atlaskit/primitives';
6
+ // Copied from: platform/packages/editor/editor-plugin-ai/src/ui/modal/styles.tsx
7
+ var selectionMarkerStyles = xcss({
8
+ content: "''",
9
+ position: 'absolute',
10
+ backgroundImage: "url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMyIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDMgMjAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMSAxSDBMMSAxLjg1NzE0VjE4LjE0MzNMMCAxOS4wMDA0SDNMMiAxOC4xNDMzVjEuODU3MTRMMyAxSDJIMVoiIGZpbGw9IiM1NzlERkYiLz4KPHJlY3QgeT0iMTkiIHdpZHRoPSIzIiBoZWlnaHQ9IjEiIGZpbGw9IiM1NzlERkYiLz4KPHJlY3Qgd2lkdGg9IjMiIGhlaWdodD0iMSIgZmlsbD0iIzU3OURGRiIvPgo8L3N2Zz4K')",
11
+ top: 'space.0',
12
+ bottom: "var(--ds-space-negative-025, -2px)",
13
+ backgroundRepeat: 'no-repeat',
14
+ backgroundPositionX: 'center',
15
+ backgroundPositionY: 'center',
16
+ backgroundSize: 'contain',
17
+ aspectRatio: '3/20',
18
+ left: '0px',
19
+ marginLeft: '-0.1em',
20
+ right: '0px',
21
+ marginRight: '-0.1em',
22
+ pointerEvents: 'none'
23
+ });
24
+ var Widget = function Widget(_ref) {
25
+ var type = _ref.type;
26
+ return /*#__PURE__*/React.createElement(Box, {
27
+ as: 'span',
28
+ xcss: selectionMarkerStyles,
29
+ testId: "selection-marker-".concat(type, "-cursor"),
30
+ contentEditable: false
31
+ });
32
+ };
33
+ var toDOM = function toDOM(type) {
34
+ var element = document.createElement('span');
35
+ element.contentEditable = 'false';
36
+ element.setAttribute('style', "position: relative;");
37
+ ReactDOM.render( /*#__PURE__*/React.createElement(Widget, {
38
+ type: type
39
+ }), element);
40
+ return element;
41
+ };
42
+ var containsText = function containsText(resolvedPos) {
43
+ var nodeBefore = resolvedPos.nodeBefore,
44
+ nodeAfter = resolvedPos.nodeAfter;
45
+ return (nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.isText) || (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.isText);
46
+ };
47
+ export var createWidgetDecoration = function createWidgetDecoration(resolvedPos, type, selection) {
48
+ // We don't want the cursor to show if it's not text selection
49
+ // ie. if it's on media selection
50
+ if (!(selection instanceof TextSelection) || containsText(resolvedPos) === false) {
51
+ return [];
52
+ }
53
+ return [Decoration.widget(resolvedPos.pos, toDOM(type), {
54
+ side: -1,
55
+ key: "".concat(type, "WidgetDecoration"),
56
+ stopEvent: function stopEvent() {
57
+ return true;
58
+ },
59
+ ignoreSelection: true
60
+ })];
61
+ };
@@ -0,0 +1,2 @@
1
+ export { selectionMarkerPlugin } from './plugin';
2
+ export type { SelectionMarkerPlugin } from './types';
@@ -0,0 +1,2 @@
1
+ import type { SelectionMarkerPlugin } from './types';
2
+ export declare const selectionMarkerPlugin: SelectionMarkerPlugin;
@@ -0,0 +1,17 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import type { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
3
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
4
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
5
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
+ import type { SelectionMarkerPlugin } from '../types';
7
+ interface PluginState {
8
+ decorations: DecorationSet;
9
+ shouldHideDecorations: boolean;
10
+ }
11
+ export declare const key: PluginKey<PluginState>;
12
+ export declare const createPlugin: (api: ExtractInjectionAPI<SelectionMarkerPlugin> | undefined) => SafePlugin<{
13
+ decorations: DecorationSet;
14
+ shouldHideDecorations: any;
15
+ }>;
16
+ export declare function dispatchShouldHideDecorations(editorView: EditorView, shouldHideDecorations: boolean): void;
17
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { NextEditorPlugin, OptionalPlugin } from '@atlaskit/editor-common/types';
2
+ import type { FocusPlugin } from '@atlaskit/editor-plugin-focus';
3
+ import type { TypeAheadPlugin } from '@atlaskit/editor-plugin-type-ahead';
4
+ export type SelectionMarkerPlugin = NextEditorPlugin<'selectionMarker', {
5
+ dependencies: [FocusPlugin, OptionalPlugin<TypeAheadPlugin>];
6
+ }>;
@@ -0,0 +1,3 @@
1
+ /** @jsx jsx */
2
+ import { jsx } from '@emotion/react';
3
+ export declare const GlobalStylesWrapper: () => jsx.JSX.Element;
@@ -0,0 +1,3 @@
1
+ import type { Selection } from '@atlaskit/editor-prosemirror/state';
2
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
3
+ export declare const selectionDecoration: (selection: Selection) => Decoration;
@@ -0,0 +1,6 @@
1
+ import type { ResolvedPos } from '@atlaskit/editor-prosemirror/model';
2
+ import type { Selection } from '@atlaskit/editor-prosemirror/state';
3
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
4
+ type SelectionType = 'anchor' | 'head';
5
+ export declare const createWidgetDecoration: (resolvedPos: ResolvedPos, type: SelectionType, selection: Selection) => Decoration[];
6
+ export {};
@@ -0,0 +1,2 @@
1
+ export { selectionMarkerPlugin } from './plugin';
2
+ export type { SelectionMarkerPlugin } from './types';
@@ -0,0 +1,2 @@
1
+ import type { SelectionMarkerPlugin } from './types';
2
+ export declare const selectionMarkerPlugin: SelectionMarkerPlugin;
@@ -0,0 +1,17 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import type { ExtractInjectionAPI } from '@atlaskit/editor-common/types';
3
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
4
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
5
+ import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
6
+ import type { SelectionMarkerPlugin } from '../types';
7
+ interface PluginState {
8
+ decorations: DecorationSet;
9
+ shouldHideDecorations: boolean;
10
+ }
11
+ export declare const key: PluginKey<PluginState>;
12
+ export declare const createPlugin: (api: ExtractInjectionAPI<SelectionMarkerPlugin> | undefined) => SafePlugin<{
13
+ decorations: DecorationSet;
14
+ shouldHideDecorations: any;
15
+ }>;
16
+ export declare function dispatchShouldHideDecorations(editorView: EditorView, shouldHideDecorations: boolean): void;
17
+ export {};
@@ -0,0 +1,9 @@
1
+ import type { NextEditorPlugin, OptionalPlugin } from '@atlaskit/editor-common/types';
2
+ import type { FocusPlugin } from '@atlaskit/editor-plugin-focus';
3
+ import type { TypeAheadPlugin } from '@atlaskit/editor-plugin-type-ahead';
4
+ export type SelectionMarkerPlugin = NextEditorPlugin<'selectionMarker', {
5
+ dependencies: [
6
+ FocusPlugin,
7
+ OptionalPlugin<TypeAheadPlugin>
8
+ ];
9
+ }>;
@@ -0,0 +1,3 @@
1
+ /** @jsx jsx */
2
+ import { jsx } from '@emotion/react';
3
+ export declare const GlobalStylesWrapper: () => jsx.JSX.Element;
@@ -0,0 +1,3 @@
1
+ import type { Selection } from '@atlaskit/editor-prosemirror/state';
2
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
3
+ export declare const selectionDecoration: (selection: Selection) => Decoration;
@@ -0,0 +1,6 @@
1
+ import type { ResolvedPos } from '@atlaskit/editor-prosemirror/model';
2
+ import type { Selection } from '@atlaskit/editor-prosemirror/state';
3
+ import { Decoration } from '@atlaskit/editor-prosemirror/view';
4
+ type SelectionType = 'anchor' | 'head';
5
+ export declare const createWidgetDecoration: (resolvedPos: ResolvedPos, type: SelectionType, selection: Selection) => Decoration[];
6
+ export {};
package/package.json ADDED
@@ -0,0 +1,94 @@
1
+ {
2
+ "name": "@atlaskit/editor-plugin-selection-marker",
3
+ "version": "1.0.0",
4
+ "description": "Selection marker plugin for @atlaskit/editor-core.",
5
+ "author": "Atlassian Pty Ltd",
6
+ "license": "Apache-2.0",
7
+ "publishConfig": {
8
+ "registry": "https://registry.npmjs.org/"
9
+ },
10
+ "atlassian": {
11
+ "team": "Editor: Lego",
12
+ "inPublicMirror": false,
13
+ "releaseModel": "continuous"
14
+ },
15
+ "repository": "https://stash.atlassian.com/projects/ATLASSIAN/repos/atlassian-frontend-monorepo",
16
+ "main": "dist/cjs/index.js",
17
+ "module": "dist/esm/index.js",
18
+ "module:es2019": "dist/es2019/index.js",
19
+ "types": "dist/types/index.d.ts",
20
+ "typesVersions": {
21
+ ">=4.5 <4.9": {
22
+ "*": [
23
+ "dist/types-ts4.5/*",
24
+ "dist/types-ts4.5/index.d.ts"
25
+ ]
26
+ }
27
+ },
28
+ "sideEffects": false,
29
+ "atlaskit:src": "src/index.ts",
30
+ "af:exports": {
31
+ ".": "./src/index.ts"
32
+ },
33
+ "dependencies": {
34
+ "@atlaskit/editor-common": "^78.10.0",
35
+ "@atlaskit/editor-plugin-focus": "^1.1.0",
36
+ "@atlaskit/editor-plugin-type-ahead": "^1.0.2",
37
+ "@atlaskit/editor-prosemirror": "3.0.0",
38
+ "@atlaskit/primitives": "^4.0.0",
39
+ "@atlaskit/tokens": "^1.38.0",
40
+ "@babel/runtime": "^7.0.0",
41
+ "@emotion/react": "^11.7.1"
42
+ },
43
+ "peerDependencies": {
44
+ "react": "^16.8.0",
45
+ "react-dom": "^16.8.0"
46
+ },
47
+ "devDependencies": {
48
+ "@af/integration-testing": "*",
49
+ "@af/visual-regression": "*",
50
+ "@atlaskit/ssr": "*",
51
+ "@atlaskit/visual-regression": "*",
52
+ "@atlassian/atlassian-frontend-prettier-config-1.0.0": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.0",
53
+ "@testing-library/react": "^12.1.5",
54
+ "react-dom": "^16.8.0",
55
+ "typescript": "~4.9.5",
56
+ "wait-for-expect": "^1.2.0"
57
+ },
58
+ "techstack": {
59
+ "@atlassian/frontend": {
60
+ "import-structure": [
61
+ "atlassian-conventions"
62
+ ],
63
+ "circular-dependencies": [
64
+ "file-and-folder-level"
65
+ ]
66
+ },
67
+ "@repo/internal": {
68
+ "dom-events": "use-bind-event-listener",
69
+ "analytics": [
70
+ "analytics-next"
71
+ ],
72
+ "design-tokens": [
73
+ "color"
74
+ ],
75
+ "theming": [
76
+ "react-context"
77
+ ],
78
+ "ui-components": [
79
+ "lite-mode"
80
+ ],
81
+ "deprecation": [
82
+ "no-deprecated-imports"
83
+ ],
84
+ "styling": [
85
+ "static",
86
+ "compiled"
87
+ ],
88
+ "imports": [
89
+ "import-no-extraneous-disable-for-examples-and-docs"
90
+ ]
91
+ }
92
+ },
93
+ "prettier": "@atlassian/atlassian-frontend-prettier-config-1.0.0"
94
+ }