@atlaskit/editor-plugin-code-block-advanced 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 (110) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE.md +11 -0
  3. package/README.md +1 -0
  4. package/afm-cc/tsconfig.json +36 -0
  5. package/build/tsconfig.json +17 -0
  6. package/dist/cjs/codeBlockAdvancedPlugin.js +33 -0
  7. package/dist/cjs/codeBlockAdvancedPluginType.js +5 -0
  8. package/dist/cjs/index.js +12 -0
  9. package/dist/cjs/nodeviews/codeBlockAdvanced.js +212 -0
  10. package/dist/cjs/nodeviews/codeBlockNodeWithToDOMFixed.js +55 -0
  11. package/dist/cjs/nodeviews/codemirrorSync/syncCMWithPM.js +38 -0
  12. package/dist/cjs/nodeviews/codemirrorSync/updateCMSelection.js +36 -0
  13. package/dist/cjs/nodeviews/extensions/bidiCharWarning.js +83 -0
  14. package/dist/cjs/nodeviews/extensions/keymap/backspace.js +33 -0
  15. package/dist/cjs/nodeviews/extensions/keymap/index.js +129 -0
  16. package/dist/cjs/nodeviews/extensions/keymap/maybeEscape.js +63 -0
  17. package/dist/cjs/nodeviews/languages/languageMap.js +100 -0
  18. package/dist/cjs/nodeviews/languages/loader.js +50 -0
  19. package/dist/cjs/nodeviews/lazyCodeBlockAdvanced.js +29 -0
  20. package/dist/cjs/pm-plugins/main.js +17 -0
  21. package/dist/cjs/ui/syntaxHighlightingTheme.js +76 -0
  22. package/dist/cjs/ui/theme.js +55 -0
  23. package/dist/es2019/codeBlockAdvancedPlugin.js +26 -0
  24. package/dist/es2019/codeBlockAdvancedPluginType.js +1 -0
  25. package/dist/es2019/index.js +4 -0
  26. package/dist/es2019/nodeviews/codeBlockAdvanced.js +170 -0
  27. package/dist/es2019/nodeviews/codeBlockNodeWithToDOMFixed.js +42 -0
  28. package/dist/es2019/nodeviews/codemirrorSync/syncCMWithPM.js +35 -0
  29. package/dist/es2019/nodeviews/codemirrorSync/updateCMSelection.js +30 -0
  30. package/dist/es2019/nodeviews/extensions/bidiCharWarning.js +53 -0
  31. package/dist/es2019/nodeviews/extensions/keymap/backspace.js +28 -0
  32. package/dist/es2019/nodeviews/extensions/keymap/index.js +107 -0
  33. package/dist/es2019/nodeviews/extensions/keymap/maybeEscape.js +57 -0
  34. package/dist/es2019/nodeviews/languages/languageMap.js +82 -0
  35. package/dist/es2019/nodeviews/languages/loader.js +37 -0
  36. package/dist/es2019/nodeviews/lazyCodeBlockAdvanced.js +18 -0
  37. package/dist/es2019/pm-plugins/main.js +11 -0
  38. package/dist/es2019/ui/syntaxHighlightingTheme.js +70 -0
  39. package/dist/es2019/ui/theme.js +49 -0
  40. package/dist/esm/codeBlockAdvancedPlugin.js +27 -0
  41. package/dist/esm/codeBlockAdvancedPluginType.js +1 -0
  42. package/dist/esm/index.js +4 -0
  43. package/dist/esm/nodeviews/codeBlockAdvanced.js +205 -0
  44. package/dist/esm/nodeviews/codeBlockNodeWithToDOMFixed.js +48 -0
  45. package/dist/esm/nodeviews/codemirrorSync/syncCMWithPM.js +32 -0
  46. package/dist/esm/nodeviews/codemirrorSync/updateCMSelection.js +30 -0
  47. package/dist/esm/nodeviews/extensions/bidiCharWarning.js +77 -0
  48. package/dist/esm/nodeviews/extensions/keymap/backspace.js +27 -0
  49. package/dist/esm/nodeviews/extensions/keymap/index.js +122 -0
  50. package/dist/esm/nodeviews/extensions/keymap/maybeEscape.js +56 -0
  51. package/dist/esm/nodeviews/languages/languageMap.js +86 -0
  52. package/dist/esm/nodeviews/languages/loader.js +44 -0
  53. package/dist/esm/nodeviews/lazyCodeBlockAdvanced.js +17 -0
  54. package/dist/esm/pm-plugins/main.js +11 -0
  55. package/dist/esm/ui/syntaxHighlightingTheme.js +70 -0
  56. package/dist/esm/ui/theme.js +49 -0
  57. package/dist/types/codeBlockAdvancedPlugin.d.ts +2 -0
  58. package/dist/types/codeBlockAdvancedPluginType.d.ts +11 -0
  59. package/dist/types/index.d.ts +2 -0
  60. package/dist/types/nodeviews/codeBlockAdvanced.d.ts +38 -0
  61. package/dist/types/nodeviews/codeBlockNodeWithToDOMFixed.d.ts +2 -0
  62. package/dist/types/nodeviews/codemirrorSync/syncCMWithPM.d.ts +17 -0
  63. package/dist/types/nodeviews/codemirrorSync/updateCMSelection.d.ts +11 -0
  64. package/dist/types/nodeviews/extensions/bidiCharWarning.d.ts +8 -0
  65. package/dist/types/nodeviews/extensions/keymap/backspace.d.ts +12 -0
  66. package/dist/types/nodeviews/extensions/keymap/index.d.ts +14 -0
  67. package/dist/types/nodeviews/extensions/keymap/maybeEscape.d.ts +17 -0
  68. package/dist/types/nodeviews/languages/languageMap.d.ts +5 -0
  69. package/dist/types/nodeviews/languages/loader.d.ts +11 -0
  70. package/dist/types/nodeviews/lazyCodeBlockAdvanced.d.ts +9 -0
  71. package/dist/types/pm-plugins/main.d.ts +10 -0
  72. package/dist/types/ui/syntaxHighlightingTheme.d.ts +2 -0
  73. package/dist/types/ui/theme.d.ts +1 -0
  74. package/dist/types-ts4.5/codeBlockAdvancedPlugin.d.ts +2 -0
  75. package/dist/types-ts4.5/codeBlockAdvancedPluginType.d.ts +15 -0
  76. package/dist/types-ts4.5/index.d.ts +2 -0
  77. package/dist/types-ts4.5/nodeviews/codeBlockAdvanced.d.ts +38 -0
  78. package/dist/types-ts4.5/nodeviews/codeBlockNodeWithToDOMFixed.d.ts +2 -0
  79. package/dist/types-ts4.5/nodeviews/codemirrorSync/syncCMWithPM.d.ts +17 -0
  80. package/dist/types-ts4.5/nodeviews/codemirrorSync/updateCMSelection.d.ts +11 -0
  81. package/dist/types-ts4.5/nodeviews/extensions/bidiCharWarning.d.ts +8 -0
  82. package/dist/types-ts4.5/nodeviews/extensions/keymap/backspace.d.ts +12 -0
  83. package/dist/types-ts4.5/nodeviews/extensions/keymap/index.d.ts +14 -0
  84. package/dist/types-ts4.5/nodeviews/extensions/keymap/maybeEscape.d.ts +17 -0
  85. package/dist/types-ts4.5/nodeviews/languages/languageMap.d.ts +5 -0
  86. package/dist/types-ts4.5/nodeviews/languages/loader.d.ts +12 -0
  87. package/dist/types-ts4.5/nodeviews/lazyCodeBlockAdvanced.d.ts +9 -0
  88. package/dist/types-ts4.5/pm-plugins/main.d.ts +10 -0
  89. package/dist/types-ts4.5/ui/syntaxHighlightingTheme.d.ts +2 -0
  90. package/dist/types-ts4.5/ui/theme.d.ts +1 -0
  91. package/docs/0-intro.tsx +56 -0
  92. package/package.json +96 -0
  93. package/src/codeBlockAdvancedPlugin.tsx +25 -0
  94. package/src/codeBlockAdvancedPluginType.ts +18 -0
  95. package/src/index.ts +5 -0
  96. package/src/nodeviews/codeBlockAdvanced.ts +223 -0
  97. package/src/nodeviews/codeBlockNodeWithToDOMFixed.ts +59 -0
  98. package/src/nodeviews/codemirrorSync/syncCMWithPM.ts +40 -0
  99. package/src/nodeviews/codemirrorSync/updateCMSelection.ts +40 -0
  100. package/src/nodeviews/extensions/bidiCharWarning.ts +72 -0
  101. package/src/nodeviews/extensions/keymap/backspace.ts +47 -0
  102. package/src/nodeviews/extensions/keymap/index.ts +118 -0
  103. package/src/nodeviews/extensions/keymap/maybeEscape.ts +69 -0
  104. package/src/nodeviews/languages/languageMap.ts +91 -0
  105. package/src/nodeviews/languages/loader.ts +44 -0
  106. package/src/nodeviews/lazyCodeBlockAdvanced.ts +32 -0
  107. package/src/pm-plugins/main.ts +22 -0
  108. package/src/ui/syntaxHighlightingTheme.ts +50 -0
  109. package/src/ui/theme.ts +52 -0
  110. package/tsconfig.json +10 -0
@@ -0,0 +1,70 @@
1
+ import { HighlightStyle } from '@codemirror/language';
2
+ import { tags } from '@lezer/highlight';
3
+ // Based on `platform/packages/design-system/code/src/internal/theme/styles.tsx`
4
+ export const highlightStyle = HighlightStyle.define([{
5
+ tag: tags.meta,
6
+ color: "var(--ds-text, #172B4D)"
7
+ }, {
8
+ tag: tags.link,
9
+ textDecoration: 'underline'
10
+ }, {
11
+ tag: tags.heading,
12
+ textDecoration: 'underline',
13
+ // Custom syntax styling to match existing styling
14
+ // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
15
+ fontWeight: "var(--ds-font-weight-bold, 700)"
16
+ }, {
17
+ tag: tags.emphasis,
18
+ fontStyle: 'italic'
19
+ }, {
20
+ tag: tags.strong,
21
+ // Custom syntax styling to match existing styling
22
+ // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
23
+ fontWeight: "var(--ds-font-weight-bold, 700)"
24
+ }, {
25
+ tag: tags.strikethrough,
26
+ textDecoration: 'line-through'
27
+ }, {
28
+ tag: tags.keyword,
29
+ color: "var(--ds-text-accent-blue, #0055CC)",
30
+ // Custom syntax styling to match existing styling
31
+ // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
32
+ fontWeight: "var(--ds-font-weight-bold, 700)"
33
+ }, {
34
+ tag: [tags.atom, tags.bool, tags.url, tags.contentSeparator, tags.labelName],
35
+ color: "var(--ds-text-accent-blue, #0055CC)"
36
+ }, {
37
+ tag: [tags.literal, tags.inserted],
38
+ color: "var(--ds-text-accent-blue, #0055CC)"
39
+ }, {
40
+ tag: [tags.string, tags.deleted],
41
+ color: "var(--ds-text-accent-green, #216E4E)"
42
+ }, {
43
+ tag: [tags.regexp, tags.escape, tags.special(tags.string)],
44
+ color: "var(--ds-text, #172B4D)"
45
+ }, {
46
+ tag: tags.definition(tags.variableName),
47
+ color: "var(--ds-text, #172B4D)"
48
+ }, {
49
+ tag: tags.local(tags.variableName),
50
+ color: "var(--ds-text, #172B4D)"
51
+ }, {
52
+ tag: [tags.typeName, tags.namespace],
53
+ color: "var(--ds-text-accent-blue, #0055CC)"
54
+ }, {
55
+ tag: tags.className,
56
+ color: "var(--ds-text-accent-purple, #5E4DB2)"
57
+ }, {
58
+ tag: [tags.special(tags.variableName), tags.macroName],
59
+ color: "var(--ds-text, #172B4D)"
60
+ }, {
61
+ tag: tags.definition(tags.propertyName),
62
+ color: "var(--ds-text, #172B4D)"
63
+ }, {
64
+ tag: tags.comment,
65
+ color: "var(--ds-text-subtlest, #626F86)",
66
+ fontStyle: 'italic'
67
+ }, {
68
+ tag: tags.invalid,
69
+ color: "var(--ds-text, #172B4D)"
70
+ }]);
@@ -0,0 +1,49 @@
1
+ import { EditorView as CodeMirror } from '@codemirror/view';
2
+ export const cmTheme = CodeMirror.theme({
3
+ '&': {
4
+ backgroundColor: "var(--ds-background-neutral, #091E420F)",
5
+ padding: '0',
6
+ marginTop: "var(--ds-space-100, 8px)",
7
+ marginBottom: "var(--ds-space-100, 8px)",
8
+ borderRadius: "var(--ds-border-radius, 4px)",
9
+ fontSize: '0.875rem',
10
+ // Custom syntax styling to match existing styling
11
+ // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
12
+ lineHeight: '1.5rem'
13
+ },
14
+ '&.cm-focused': {
15
+ outline: 'none'
16
+ },
17
+ '.cm-line': {
18
+ padding: '0'
19
+ },
20
+ '&.cm-editor.code-block.danger': {
21
+ backgroundColor: "var(--ds-background-danger, #FFECEB)"
22
+ },
23
+ '.cm-content': {
24
+ cursor: 'text',
25
+ caretColor: "var(--ds-text, #172B4D)",
26
+ margin: "var(--ds-space-100, 8px)",
27
+ padding: "var(--ds-space-0, 0px)"
28
+ },
29
+ '.cm-scroller': {
30
+ backgroundColor: "var(--ds-background-neutral, #091E420F)",
31
+ // Custom syntax styling to match existing styling
32
+ // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
33
+ lineHeight: 'unset',
34
+ fontFamily: "var(--ds-font-family-code, ui-monospace, Menlo, \"Segoe UI Mono\", \"Ubuntu Mono\", monospace)"
35
+ },
36
+ '&.cm-focused .cm-cursor': {
37
+ borderLeftColor: "var(--ds-text, #172B4D)"
38
+ },
39
+ '.cm-gutters': {
40
+ backgroundColor: "var(--ds-background-neutral, #091E420F)",
41
+ border: 'none',
42
+ padding: "var(--ds-space-100, 8px)"
43
+ },
44
+ '.cm-lineNumbers .cm-gutterElement': {
45
+ paddingLeft: "var(--ds-space-0, 0px)",
46
+ paddingRight: "var(--ds-space-0, 0px)",
47
+ minWidth: 'unset'
48
+ }
49
+ });
@@ -0,0 +1,27 @@
1
+ import { codeBlockNodeWithFixedToDOM } from './nodeviews/codeBlockNodeWithToDOMFixed';
2
+ import { createPlugin } from './pm-plugins/main';
3
+ export var codeBlockAdvancedPlugin = function codeBlockAdvancedPlugin(_ref) {
4
+ var api = _ref.api,
5
+ config = _ref.config;
6
+ return {
7
+ name: 'codeBlockAdvanced',
8
+ nodes: function nodes() {
9
+ return [{
10
+ name: 'codeBlock',
11
+ node: codeBlockNodeWithFixedToDOM()
12
+ }];
13
+ },
14
+ pmPlugins: function pmPlugins() {
15
+ return [{
16
+ name: 'codeBlockAdvancedPlugin',
17
+ plugin: function plugin() {
18
+ var _config$extensions;
19
+ return createPlugin({
20
+ api: api,
21
+ extensions: (_config$extensions = config === null || config === void 0 ? void 0 : config.extensions) !== null && _config$extensions !== void 0 ? _config$extensions : []
22
+ });
23
+ }
24
+ }];
25
+ }
26
+ };
27
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ /* eslint-disable @atlaskit/editor/no-re-export */
2
+ // Entry file in package.json
3
+
4
+ export { codeBlockAdvancedPlugin } from './codeBlockAdvancedPlugin';
@@ -0,0 +1,205 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
5
+ import { closeBrackets } from '@codemirror/autocomplete';
6
+ import { syntaxHighlighting } from '@codemirror/language';
7
+ import { Compartment, EditorSelection } from '@codemirror/state';
8
+ import { EditorView as CodeMirror, lineNumbers } from '@codemirror/view';
9
+ import { isCodeBlockWordWrapEnabled } from '@atlaskit/editor-common/code-block';
10
+ import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
11
+ import { highlightStyle } from '../ui/syntaxHighlightingTheme';
12
+ import { cmTheme } from '../ui/theme';
13
+ import { syncCMWithPM } from './codemirrorSync/syncCMWithPM';
14
+ import { updateCMSelection } from './codemirrorSync/updateCMSelection';
15
+ import { bidiCharWarningExtension } from './extensions/bidiCharWarning';
16
+ import { keymapExtension } from './extensions/keymap';
17
+ import { LanguageLoader } from './languages/loader';
18
+ // Based on: https://prosemirror.net/examples/codemirror/
19
+ var CodeBlockAdvancedNodeView = /*#__PURE__*/function () {
20
+ // eslint-disable-next-line @typescript-eslint/max-params
21
+ function CodeBlockAdvancedNodeView(node, view, getPos, config) {
22
+ var _config$api,
23
+ _this = this,
24
+ _config$api2;
25
+ _classCallCheck(this, CodeBlockAdvancedNodeView);
26
+ _defineProperty(this, "lineWrappingCompartment", new Compartment());
27
+ _defineProperty(this, "languageCompartment", new Compartment());
28
+ _defineProperty(this, "readOnlyCompartment", new Compartment());
29
+ _defineProperty(this, "maybeTryingToReachNodeSelection", false);
30
+ _defineProperty(this, "wordWrappingEnabled", false);
31
+ this.node = node;
32
+ this.view = view;
33
+ this.getPos = getPos;
34
+ this.selectionAPI = (_config$api = config.api) === null || _config$api === void 0 || (_config$api = _config$api.selection) === null || _config$api === void 0 ? void 0 : _config$api.actions;
35
+ var getNode = function getNode() {
36
+ return _this.node;
37
+ };
38
+ var onMaybeNodeSelection = function onMaybeNodeSelection() {
39
+ return _this.maybeTryingToReachNodeSelection = true;
40
+ };
41
+ this.cleanupDisabledState = (_config$api2 = config.api) === null || _config$api2 === void 0 || (_config$api2 = _config$api2.editorDisabled) === null || _config$api2 === void 0 ? void 0 : _config$api2.sharedState.onChange(function () {
42
+ _this.updateReadonlyState();
43
+ });
44
+ this.languageLoader = new LanguageLoader(function (lang) {
45
+ _this.updating = true;
46
+ _this.cm.dispatch({
47
+ effects: _this.languageCompartment.reconfigure(lang)
48
+ });
49
+ _this.updating = false;
50
+ });
51
+ this.cm = new CodeMirror({
52
+ doc: this.node.textContent,
53
+ extensions: [].concat(_toConsumableArray(config.extensions), [this.lineWrappingCompartment.of([]), this.languageCompartment.of([]), keymapExtension({
54
+ view: view,
55
+ getPos: getPos,
56
+ getNode: getNode,
57
+ selectCodeBlockNode: this.selectCodeBlockNode.bind(this),
58
+ onMaybeNodeSelection: onMaybeNodeSelection
59
+ }), cmTheme, syntaxHighlighting(highlightStyle), lineNumbers(), CodeMirror.updateListener.of(function (update) {
60
+ return _this.forwardUpdate(update);
61
+ }), this.readOnlyCompartment.of(CodeMirror.editable.of(this.view.editable)), closeBrackets(), CodeMirror.editorAttributes.of({
62
+ class: 'code-block'
63
+ }), bidiCharWarningExtension])
64
+ });
65
+
66
+ // The editor's outer node is our DOM representation
67
+ this.dom = this.cm.dom;
68
+
69
+ // This flag is used to avoid an update loop between the outer and
70
+ // inner editor
71
+ this.updating = false;
72
+ this.updateLanguage();
73
+ }
74
+ return _createClass(CodeBlockAdvancedNodeView, [{
75
+ key: "destroy",
76
+ value: function destroy() {
77
+ var _this$cleanupDisabled;
78
+ (_this$cleanupDisabled = this.cleanupDisabledState) === null || _this$cleanupDisabled === void 0 || _this$cleanupDisabled.call(this);
79
+ }
80
+ }, {
81
+ key: "forwardUpdate",
82
+ value: function forwardUpdate(update) {
83
+ var _this$getPos, _this$getPos2;
84
+ if (this.updating || !this.cm.hasFocus) {
85
+ return;
86
+ }
87
+ var offset = ((_this$getPos = (_this$getPos2 = this.getPos) === null || _this$getPos2 === void 0 ? void 0 : _this$getPos2.call(this)) !== null && _this$getPos !== void 0 ? _this$getPos : 0) + 1;
88
+ syncCMWithPM({
89
+ view: this.view,
90
+ update: update,
91
+ offset: offset
92
+ });
93
+ }
94
+ }, {
95
+ key: "setSelection",
96
+ value: function setSelection(anchor, head) {
97
+ if (!this.maybeTryingToReachNodeSelection) {
98
+ this.cm.focus();
99
+ }
100
+ this.updating = true;
101
+ this.cm.dispatch({
102
+ selection: {
103
+ anchor: anchor,
104
+ head: head
105
+ }
106
+ });
107
+ this.updating = false;
108
+ }
109
+ }, {
110
+ key: "updateReadonlyState",
111
+ value: function updateReadonlyState() {
112
+ this.updating = true;
113
+ this.cm.dispatch({
114
+ effects: this.readOnlyCompartment.reconfigure(CodeMirror.editable.of(this.view.editable))
115
+ });
116
+ this.updating = false;
117
+ }
118
+ }, {
119
+ key: "updateLanguage",
120
+ value: function updateLanguage() {
121
+ this.languageLoader.updateLanguage(this.node.attrs.language);
122
+ }
123
+ }, {
124
+ key: "selectCodeBlockNode",
125
+ value: function selectCodeBlockNode(relativeSelectionPos) {
126
+ var _this$selectionAPI, _this$getPos3, _this$getPos4;
127
+ var tr = (_this$selectionAPI = this.selectionAPI) === null || _this$selectionAPI === void 0 ? void 0 : _this$selectionAPI.selectNearNode({
128
+ selectionRelativeToNode: relativeSelectionPos,
129
+ selection: NodeSelection.create(this.view.state.doc, (_this$getPos3 = (_this$getPos4 = this.getPos) === null || _this$getPos4 === void 0 ? void 0 : _this$getPos4.call(this)) !== null && _this$getPos3 !== void 0 ? _this$getPos3 : 0)
130
+ })(this.view.state);
131
+ if (tr) {
132
+ this.view.dispatch(tr);
133
+ }
134
+ }
135
+ }, {
136
+ key: "updateWordWrap",
137
+ value: function updateWordWrap(node) {
138
+ if (this.wordWrappingEnabled !== isCodeBlockWordWrapEnabled(node)) {
139
+ this.updating = true;
140
+ this.cm.dispatch({
141
+ effects: [this.lineWrappingCompartment.reconfigure(isCodeBlockWordWrapEnabled(node) ? CodeMirror.lineWrapping : [])]
142
+ });
143
+ this.updating = false;
144
+ this.wordWrappingEnabled = !this.wordWrappingEnabled;
145
+ }
146
+ }
147
+ }, {
148
+ key: "update",
149
+ value: function update(node) {
150
+ var _this2 = this;
151
+ this.maybeTryingToReachNodeSelection = false;
152
+ if (node.type !== this.node.type) {
153
+ return false;
154
+ }
155
+ this.updateWordWrap(node);
156
+ this.node = node;
157
+ if (this.updating) {
158
+ return true;
159
+ }
160
+ this.updateLanguage();
161
+ var newText = node.textContent,
162
+ curText = this.cm.state.doc.toString();
163
+ updateCMSelection(curText, newText, function (tr) {
164
+ _this2.updating = true;
165
+ _this2.cm.dispatch(tr);
166
+ _this2.updating = false;
167
+ });
168
+ return true;
169
+ }
170
+ }, {
171
+ key: "stopEvent",
172
+ value: function stopEvent(e) {
173
+ var _this3 = this,
174
+ _this$getPos5;
175
+ if (e instanceof MouseEvent && e.type === 'mousedown') {
176
+ // !Warning: Side effect!
177
+ // CodeMirror on blur updates the dom observer with a `setTimeout(..., 10);`
178
+ // We need to select the nodeview after this has taken place to ensure
179
+ // ProseMirror takes over
180
+ // https://github.com/codemirror/view/commit/70a9a253df04a57004247b9463198c17832f92f4#diff-cb8cbffa623ff0975389e7e8c315e69d5e10345239ffe2c9b4b7986a56ad95efR720
181
+ setTimeout(function () {
182
+ // Ensure the CM selection is reset - if we have a ranged selection when we do node selection can
183
+ // cause funky behaviour
184
+ _this3.updating = true;
185
+ _this3.cm.dispatch({
186
+ selection: EditorSelection.create([EditorSelection.cursor(0)], 0)
187
+ });
188
+ _this3.updating = false;
189
+ _this3.selectCodeBlockNode(undefined);
190
+ _this3.view.focus();
191
+ }, 20);
192
+ }
193
+ // If we have selected the node we should not stop these events
194
+ if (e instanceof KeyboardEvent && this.view.state.selection instanceof NodeSelection && this.view.state.selection.from === ((_this$getPos5 = this.getPos) === null || _this$getPos5 === void 0 ? void 0 : _this$getPos5.call(this))) {
195
+ return false;
196
+ }
197
+ return true;
198
+ }
199
+ }]);
200
+ }();
201
+ export var getCodeBlockAdvancedNodeView = function getCodeBlockAdvancedNodeView(props) {
202
+ return function (node, view, getPos) {
203
+ return new CodeBlockAdvancedNodeView(node, view, getPos, props);
204
+ };
205
+ };
@@ -0,0 +1,48 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ 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; }
3
+ 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; }
4
+ import { codeBlock } from '@atlaskit/adf-schema';
5
+ import { CodeBlockSharedCssClassName } from '@atlaskit/editor-common/styles';
6
+ var codeBlockClassNames = {
7
+ container: CodeBlockSharedCssClassName.CODEBLOCK_CONTAINER,
8
+ start: CodeBlockSharedCssClassName.CODEBLOCK_START,
9
+ end: CodeBlockSharedCssClassName.CODEBLOCK_END,
10
+ contentWrapper: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT_WRAPPER,
11
+ contentWrapped: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT_WRAPPED,
12
+ gutter: CodeBlockSharedCssClassName.CODEBLOCK_LINE_NUMBER_GUTTER,
13
+ content: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT,
14
+ lineNumberWidget: CodeBlockSharedCssClassName.CODEBLOCK_CONTAINER_LINE_NUMBER_WIDGET
15
+ };
16
+
17
+ // From: `packages/editor/editor-plugin-code-block/src/nodeviews/code-block.ts`
18
+ var _toDOM = function toDOM(node, contentEditable, formattedAriaLabel) {
19
+ return ['div', {
20
+ class: codeBlockClassNames.container
21
+ }, ['div', {
22
+ class: codeBlockClassNames.start,
23
+ contenteditable: 'false'
24
+ }], ['div', {
25
+ class: codeBlockClassNames.contentWrapper
26
+ }, ['div', {
27
+ class: codeBlockClassNames.gutter,
28
+ contenteditable: 'false'
29
+ }], ['div', {
30
+ class: codeBlockClassNames.content
31
+ }, ['code', {
32
+ 'data-language': node.attrs.language || '',
33
+ spellcheck: 'false',
34
+ contenteditable: contentEditable ? 'true' : 'false',
35
+ 'data-testid': 'code-block--code',
36
+ 'aria-label': formattedAriaLabel
37
+ }, 0]]], ['div', {
38
+ class: codeBlockClassNames.end,
39
+ contenteditable: 'false'
40
+ }]];
41
+ };
42
+ export var codeBlockNodeWithFixedToDOM = function codeBlockNodeWithFixedToDOM() {
43
+ return _objectSpread(_objectSpread({}, codeBlock), {}, {
44
+ toDOM: function toDOM(node) {
45
+ return _toDOM(node, false, '');
46
+ }
47
+ });
48
+ };
@@ -0,0 +1,32 @@
1
+ import { TextSelection } from '@atlaskit/editor-prosemirror/state';
2
+ /**
3
+ *
4
+ * Synchronises the CodeMirror update changes with the Prosemirror editor
5
+ *
6
+ * @param props.view EditorView - Prosemirror EditorView
7
+ * @param props.update ViewUpdate - CodeMirror ViewUpdate
8
+ * @param props.offset number - position where the code block starts in prosemirror
9
+ */
10
+ export var syncCMWithPM = function syncCMWithPM(_ref) {
11
+ var view = _ref.view,
12
+ update = _ref.update,
13
+ offset = _ref.offset;
14
+ var main = update.state.selection.main;
15
+ var selFrom = offset + main.from;
16
+ var selTo = offset + main.to;
17
+ var pmSel = view.state.selection;
18
+ if (update.docChanged || pmSel.from !== selFrom || pmSel.to !== selTo) {
19
+ var tr = view.state.tr;
20
+ // eslint-disable-next-line @typescript-eslint/max-params
21
+ update.changes.iterChanges(function (fromA, toA, fromB, toB, text) {
22
+ if (text.length) {
23
+ tr.replaceWith(offset + fromA, offset + toA, view.state.schema.text(text.toString()));
24
+ } else {
25
+ tr.delete(offset + fromA, offset + toA);
26
+ }
27
+ offset += toB - fromB - (toA - fromA);
28
+ });
29
+ tr.setSelection(TextSelection.create(tr.doc, selFrom, selTo));
30
+ view.dispatch(tr);
31
+ }
32
+ };
@@ -0,0 +1,30 @@
1
+ /**
2
+ *
3
+ * Compares the updated text with the current to determine the transaction to fire
4
+ * in the CodeMirror editor.
5
+ *
6
+ * @param curText string - the current CodeMirror text
7
+ * @param newText string - the new CodeMirror text
8
+ * @param updateCallback Callback to process the CodeMirror transaction
9
+ */
10
+ export var updateCMSelection = function updateCMSelection(curText, newText, updateCallback) {
11
+ if (newText !== curText) {
12
+ var start = 0,
13
+ curEnd = curText.length,
14
+ newEnd = newText.length;
15
+ while (start < curEnd && curText.charCodeAt(start) === newText.charCodeAt(start)) {
16
+ ++start;
17
+ }
18
+ while (curEnd > start && newEnd > start && curText.charCodeAt(curEnd - 1) === newText.charCodeAt(newEnd - 1)) {
19
+ curEnd--;
20
+ newEnd--;
21
+ }
22
+ updateCallback({
23
+ changes: {
24
+ from: start,
25
+ to: curEnd,
26
+ insert: newText.slice(start, newEnd)
27
+ }
28
+ });
29
+ }
30
+ };
@@ -0,0 +1,77 @@
1
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
2
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
3
+ import _inherits from "@babel/runtime/helpers/inherits";
4
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
5
+ import _createClass from "@babel/runtime/helpers/createClass";
6
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
7
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
8
+ /**
9
+ * Code based on warning from @atlaskit/code
10
+ */
11
+ import { EditorView as CodeMirror, ViewPlugin, Decoration, MatchDecorator, WidgetType } from '@codemirror/view';
12
+ var bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/g;
13
+ var placeholderMatcher = new MatchDecorator({
14
+ regexp: bidiCharacterRegex,
15
+ decoration: function decoration(match) {
16
+ return Decoration.replace({
17
+ widget: new PlaceholderWidget(match[0])
18
+ });
19
+ }
20
+ });
21
+ export var bidiCharWarningExtension = ViewPlugin.fromClass( /*#__PURE__*/function () {
22
+ function _class(view) {
23
+ _classCallCheck(this, _class);
24
+ this.placeholders = placeholderMatcher.createDeco(view);
25
+ }
26
+ return _createClass(_class, [{
27
+ key: "update",
28
+ value: function update(_update) {
29
+ this.placeholders = placeholderMatcher.updateDeco(_update, this.placeholders);
30
+ }
31
+ }]);
32
+ }(), {
33
+ decorations: function decorations(instance) {
34
+ return instance.placeholders;
35
+ },
36
+ provide: function provide(plugin) {
37
+ return CodeMirror.atomicRanges.of(function (view) {
38
+ var _view$plugin;
39
+ return ((_view$plugin = view.plugin(plugin)) === null || _view$plugin === void 0 ? void 0 : _view$plugin.placeholders) || Decoration.none;
40
+ });
41
+ }
42
+ });
43
+ function getBidiCharacterCode(bidiCharacter) {
44
+ var _bidiCharacter$codePo;
45
+ var bidiCharacterCode = (_bidiCharacter$codePo = bidiCharacter.codePointAt(0)) === null || _bidiCharacter$codePo === void 0 ? void 0 : _bidiCharacter$codePo.toString(16);
46
+ return "U+".concat(bidiCharacterCode);
47
+ }
48
+ var PlaceholderWidget = /*#__PURE__*/function (_WidgetType) {
49
+ function PlaceholderWidget(name) {
50
+ var _this;
51
+ _classCallCheck(this, PlaceholderWidget);
52
+ _this = _callSuper(this, PlaceholderWidget);
53
+ _this.name = name;
54
+ return _this;
55
+ }
56
+ _inherits(PlaceholderWidget, _WidgetType);
57
+ return _createClass(PlaceholderWidget, [{
58
+ key: "eq",
59
+ value: function eq(other) {
60
+ return this.name === other.name;
61
+ }
62
+ }, {
63
+ key: "toDOM",
64
+ value: function toDOM() {
65
+ var elt = document.createElement('span');
66
+ elt.setAttribute('data-bidi-character-code', this.name);
67
+ elt.style.cssText = "\n padding: 0 3px;\n color: ".concat("var(--ds-text-warning, #A54800)", ";\n\t\t\tbackground: ", "var(--ds-background-warning, #FFF7D6)", ";\n\t\t\taria-hidden=\"true\"");
68
+ elt.textContent = "<".concat(getBidiCharacterCode(this.name), ">");
69
+ return elt;
70
+ }
71
+ }, {
72
+ key: "ignoreEvent",
73
+ value: function ignoreEvent() {
74
+ return false;
75
+ }
76
+ }]);
77
+ }(WidgetType);
@@ -0,0 +1,27 @@
1
+ import { TextSelection } from '@atlaskit/editor-prosemirror/state';
2
+ export var backspaceKeymap = function backspaceKeymap(_ref) {
3
+ var _getPos;
4
+ var cm = _ref.cm,
5
+ view = _ref.view,
6
+ getPos = _ref.getPos,
7
+ getNode = _ref.getNode;
8
+ var ranges = cm.state.selection.ranges;
9
+ if (ranges.length > 1) {
10
+ return false;
11
+ }
12
+ var selection = ranges[0];
13
+ if (selection && (!selection.empty || selection.anchor > 0)) {
14
+ return false;
15
+ }
16
+ if (cm.state.doc.lines >= 2) {
17
+ return false;
18
+ }
19
+ var state = view.state;
20
+ var pos = (_getPos = getPos()) !== null && _getPos !== void 0 ? _getPos : 0;
21
+ var node = getNode();
22
+ var tr = state.tr.replaceWith(pos, pos + node.nodeSize, state.schema.nodes.paragraph.createChecked({}, node.content));
23
+ tr.setSelection(TextSelection.near(tr.doc.resolve(pos)));
24
+ view.dispatch(tr);
25
+ view.focus();
26
+ return true;
27
+ };