@atlaskit/editor-plugin-code-block 0.1.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 (126) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE.md +13 -0
  3. package/README.md +30 -0
  4. package/dist/cjs/actions.js +201 -0
  5. package/dist/cjs/ide-ux/bracket-handling.js +35 -0
  6. package/dist/cjs/ide-ux/commands.js +115 -0
  7. package/dist/cjs/ide-ux/line-handling.js +81 -0
  8. package/dist/cjs/ide-ux/paired-character-handling.js +23 -0
  9. package/dist/cjs/ide-ux/quote-handling.js +40 -0
  10. package/dist/cjs/index.js +13 -0
  11. package/dist/cjs/language-list.js +62 -0
  12. package/dist/cjs/nodeviews/code-block.js +153 -0
  13. package/dist/cjs/plugin-key.js +8 -0
  14. package/dist/cjs/plugin.js +120 -0
  15. package/dist/cjs/pm-plugins/actions.js +10 -0
  16. package/dist/cjs/pm-plugins/codeBlockCopySelectionPlugin.js +113 -0
  17. package/dist/cjs/pm-plugins/ide-ux.js +132 -0
  18. package/dist/cjs/pm-plugins/input-rule.js +68 -0
  19. package/dist/cjs/pm-plugins/keymaps.js +66 -0
  20. package/dist/cjs/pm-plugins/main-state.js +10 -0
  21. package/dist/cjs/pm-plugins/main.js +114 -0
  22. package/dist/cjs/refresh-browser-selection.js +29 -0
  23. package/dist/cjs/toolbar.js +131 -0
  24. package/dist/cjs/transform-to-code-block.js +84 -0
  25. package/dist/cjs/types.js +5 -0
  26. package/dist/cjs/ui/class-names.js +15 -0
  27. package/dist/cjs/utils.js +28 -0
  28. package/dist/es2019/actions.js +211 -0
  29. package/dist/es2019/ide-ux/bracket-handling.js +27 -0
  30. package/dist/es2019/ide-ux/commands.js +115 -0
  31. package/dist/es2019/ide-ux/line-handling.js +75 -0
  32. package/dist/es2019/ide-ux/paired-character-handling.js +12 -0
  33. package/dist/es2019/ide-ux/quote-handling.js +32 -0
  34. package/dist/es2019/index.js +1 -0
  35. package/dist/es2019/language-list.js +51 -0
  36. package/dist/es2019/nodeviews/code-block.js +126 -0
  37. package/dist/es2019/plugin-key.js +2 -0
  38. package/dist/es2019/plugin.js +104 -0
  39. package/dist/es2019/pm-plugins/actions.js +4 -0
  40. package/dist/es2019/pm-plugins/codeBlockCopySelectionPlugin.js +101 -0
  41. package/dist/es2019/pm-plugins/ide-ux.js +135 -0
  42. package/dist/es2019/pm-plugins/input-rule.js +60 -0
  43. package/dist/es2019/pm-plugins/keymaps.js +58 -0
  44. package/dist/es2019/pm-plugins/main-state.js +2 -0
  45. package/dist/es2019/pm-plugins/main.js +102 -0
  46. package/dist/es2019/refresh-browser-selection.js +25 -0
  47. package/dist/es2019/toolbar.js +108 -0
  48. package/dist/es2019/transform-to-code-block.js +79 -0
  49. package/dist/es2019/types.js +1 -0
  50. package/dist/es2019/ui/class-names.js +9 -0
  51. package/dist/es2019/utils.js +4 -0
  52. package/dist/esm/actions.js +191 -0
  53. package/dist/esm/ide-ux/bracket-handling.js +29 -0
  54. package/dist/esm/ide-ux/commands.js +107 -0
  55. package/dist/esm/ide-ux/line-handling.js +73 -0
  56. package/dist/esm/ide-ux/paired-character-handling.js +16 -0
  57. package/dist/esm/ide-ux/quote-handling.js +34 -0
  58. package/dist/esm/index.js +1 -0
  59. package/dist/esm/language-list.js +52 -0
  60. package/dist/esm/nodeviews/code-block.js +146 -0
  61. package/dist/esm/plugin-key.js +2 -0
  62. package/dist/esm/plugin.js +113 -0
  63. package/dist/esm/pm-plugins/actions.js +4 -0
  64. package/dist/esm/pm-plugins/codeBlockCopySelectionPlugin.js +103 -0
  65. package/dist/esm/pm-plugins/ide-ux.js +126 -0
  66. package/dist/esm/pm-plugins/input-rule.js +62 -0
  67. package/dist/esm/pm-plugins/keymaps.js +59 -0
  68. package/dist/esm/pm-plugins/main-state.js +4 -0
  69. package/dist/esm/pm-plugins/main.js +107 -0
  70. package/dist/esm/refresh-browser-selection.js +25 -0
  71. package/dist/esm/toolbar.js +121 -0
  72. package/dist/esm/transform-to-code-block.js +77 -0
  73. package/dist/esm/types.js +1 -0
  74. package/dist/esm/ui/class-names.js +9 -0
  75. package/dist/esm/utils.js +4 -0
  76. package/dist/types/actions.d.ts +18 -0
  77. package/dist/types/ide-ux/bracket-handling.d.ts +12 -0
  78. package/dist/types/ide-ux/commands.d.ts +7 -0
  79. package/dist/types/ide-ux/line-handling.d.ts +25 -0
  80. package/dist/types/ide-ux/paired-character-handling.d.ts +2 -0
  81. package/dist/types/ide-ux/quote-handling.d.ts +12 -0
  82. package/dist/types/index.d.ts +3 -0
  83. package/dist/types/language-list.d.ts +942 -0
  84. package/dist/types/nodeviews/code-block.d.ts +21 -0
  85. package/dist/types/plugin-key.d.ts +2 -0
  86. package/dist/types/plugin.d.ts +19 -0
  87. package/dist/types/pm-plugins/actions.d.ts +4 -0
  88. package/dist/types/pm-plugins/codeBlockCopySelectionPlugin.d.ts +11 -0
  89. package/dist/types/pm-plugins/ide-ux.d.ts +5 -0
  90. package/dist/types/pm-plugins/input-rule.d.ts +3 -0
  91. package/dist/types/pm-plugins/keymaps.d.ts +4 -0
  92. package/dist/types/pm-plugins/main-state.d.ts +8 -0
  93. package/dist/types/pm-plugins/main.d.ts +10 -0
  94. package/dist/types/refresh-browser-selection.d.ts +5 -0
  95. package/dist/types/toolbar.d.ts +14 -0
  96. package/dist/types/transform-to-code-block.d.ts +3 -0
  97. package/dist/types/types.d.ts +6 -0
  98. package/dist/types/ui/class-names.d.ts +8 -0
  99. package/dist/types/utils.d.ts +4 -0
  100. package/dist/types-ts4.5/actions.d.ts +18 -0
  101. package/dist/types-ts4.5/ide-ux/bracket-handling.d.ts +12 -0
  102. package/dist/types-ts4.5/ide-ux/commands.d.ts +7 -0
  103. package/dist/types-ts4.5/ide-ux/line-handling.d.ts +25 -0
  104. package/dist/types-ts4.5/ide-ux/paired-character-handling.d.ts +2 -0
  105. package/dist/types-ts4.5/ide-ux/quote-handling.d.ts +12 -0
  106. package/dist/types-ts4.5/index.d.ts +3 -0
  107. package/dist/types-ts4.5/language-list.d.ts +1641 -0
  108. package/dist/types-ts4.5/nodeviews/code-block.d.ts +21 -0
  109. package/dist/types-ts4.5/plugin-key.d.ts +2 -0
  110. package/dist/types-ts4.5/plugin.d.ts +19 -0
  111. package/dist/types-ts4.5/pm-plugins/actions.d.ts +4 -0
  112. package/dist/types-ts4.5/pm-plugins/codeBlockCopySelectionPlugin.d.ts +14 -0
  113. package/dist/types-ts4.5/pm-plugins/ide-ux.d.ts +5 -0
  114. package/dist/types-ts4.5/pm-plugins/input-rule.d.ts +3 -0
  115. package/dist/types-ts4.5/pm-plugins/keymaps.d.ts +4 -0
  116. package/dist/types-ts4.5/pm-plugins/main-state.d.ts +8 -0
  117. package/dist/types-ts4.5/pm-plugins/main.d.ts +10 -0
  118. package/dist/types-ts4.5/refresh-browser-selection.d.ts +5 -0
  119. package/dist/types-ts4.5/toolbar.d.ts +14 -0
  120. package/dist/types-ts4.5/transform-to-code-block.d.ts +3 -0
  121. package/dist/types-ts4.5/types.d.ts +6 -0
  122. package/dist/types-ts4.5/ui/class-names.d.ts +8 -0
  123. package/dist/types-ts4.5/utils.d.ts +4 -0
  124. package/package.json +99 -0
  125. package/report.api.md +73 -0
  126. package/tmp/api-report-tmp.d.ts +45 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ # @atlaskit/editor-plugin-code-block
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,30 @@
1
+ # Editor plugin code block
2
+
3
+ Code Block 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
+ - **Install** - *yarn add @atlaskit/editor-plugin-code-block*
11
+ - **npm** - [@atlaskit/editor-plugin-code-block](https://www.npmjs.com/package/@atlaskit/editor-plugin-code-block)
12
+ - **Source** - [Bitbucket](https://bitbucket.org/atlassian/atlassian-frontend/src/master/packages/editor/editor-plugin-code-block)
13
+ - **Bundle** - [unpkg.com](https://unpkg.com/@atlaskit/editor-plugin-code-block/dist/)
14
+
15
+ ## Usage
16
+ ---
17
+ **Internal use only**
18
+
19
+ @atlaskit/editor-plugin-code-block is intended for internal use by the @atlaskit/editor-core and as a plugin dependency of the Editor within your product.
20
+
21
+ Direct use of this component is not supported.
22
+
23
+ Please see [Atlaskit - Editor plugin code block](https://atlaskit.atlassian.com/packages/editor/editor-plugin-code-block) for documentation and examples for this package.
24
+
25
+ ## Support
26
+ ---
27
+ 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.
28
+ ## License
29
+ ---
30
+ Please see [Atlassian Frontend - License](https://hello.atlassian.net/wiki/spaces/AF/pages/2589099144/Documentation#License) for more licensing information.
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.copyContentToClipboard = exports.changeLanguage = void 0;
7
+ exports.createInsertCodeBlockTransaction = createInsertCodeBlockTransaction;
8
+ exports.ignoreFollowingMutations = void 0;
9
+ exports.insertCodeBlockWithAnalytics = insertCodeBlockWithAnalytics;
10
+ exports.resetShouldIgnoreFollowingMutations = exports.resetCopiedState = exports.removeCodeBlock = void 0;
11
+ var _analytics = require("@atlaskit/editor-common/analytics");
12
+ var _clipboard = require("@atlaskit/editor-common/clipboard");
13
+ var _editorAnalytics = require("@atlaskit/editor-common/editor-analytics");
14
+ var _insert = require("@atlaskit/editor-common/insert");
15
+ var _state = require("@atlaskit/editor-prosemirror/state");
16
+ var _utils = require("@atlaskit/editor-prosemirror/utils");
17
+ var _pluginKey = require("./plugin-key");
18
+ var _actions = require("./pm-plugins/actions");
19
+ var _codeBlockCopySelectionPlugin = require("./pm-plugins/codeBlockCopySelectionPlugin");
20
+ var _transformToCodeBlock = require("./transform-to-code-block");
21
+ var removeCodeBlock = exports.removeCodeBlock = function removeCodeBlock(state, dispatch) {
22
+ var nodes = state.schema.nodes,
23
+ tr = state.tr;
24
+ if (dispatch) {
25
+ var removeTr = tr;
26
+ if ((0, _utils.findSelectedNodeOfType)(nodes.codeBlock)(tr.selection)) {
27
+ removeTr = (0, _utils.removeSelectedNode)(tr);
28
+ } else {
29
+ removeTr = (0, _utils.removeParentNodeOfType)(nodes.codeBlock)(tr);
30
+ }
31
+ dispatch(removeTr);
32
+ }
33
+ return true;
34
+ };
35
+ var changeLanguage = exports.changeLanguage = function changeLanguage(editorAnalyticsAPI) {
36
+ return function (language) {
37
+ return function (state, dispatch) {
38
+ var _pluginKey$getState;
39
+ var codeBlock = state.schema.nodes.codeBlock;
40
+ var pos = (_pluginKey$getState = _pluginKey.pluginKey.getState(state)) === null || _pluginKey$getState === void 0 ? void 0 : _pluginKey$getState.pos;
41
+ if (typeof pos !== 'number') {
42
+ return false;
43
+ }
44
+ var tr = state.tr.setNodeMarkup(pos, codeBlock, {
45
+ language: language
46
+ }).setMeta('scrollIntoView', false);
47
+ var selection = (0, _utils.isNodeSelection)(state.selection) ? _state.NodeSelection.create(tr.doc, pos) : tr.selection;
48
+ var result = tr.setSelection(selection);
49
+ if (dispatch) {
50
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
51
+ action: _analytics.ACTION.LANGUAGE_SELECTED,
52
+ actionSubject: _analytics.ACTION_SUBJECT.CODE_BLOCK,
53
+ attributes: {
54
+ language: language
55
+ },
56
+ eventType: _analytics.EVENT_TYPE.TRACK
57
+ })(result);
58
+ dispatch(result);
59
+ }
60
+ return true;
61
+ };
62
+ };
63
+ };
64
+ var copyContentToClipboard = exports.copyContentToClipboard = function copyContentToClipboard(state, dispatch) {
65
+ var nodes = state.schema.nodes,
66
+ tr = state.tr;
67
+ var codeBlock = (0, _utils.findParentNodeOfType)(nodes.codeBlock)(tr.selection);
68
+ var textContent = codeBlock && codeBlock.node.textContent;
69
+ if (textContent) {
70
+ (0, _clipboard.copyToClipboard)(textContent);
71
+ var copyToClipboardTr = tr;
72
+ copyToClipboardTr.setMeta(_pluginKey.pluginKey, {
73
+ type: _actions.ACTIONS.SET_COPIED_TO_CLIPBOARD,
74
+ data: true
75
+ });
76
+ copyToClipboardTr.setMeta(_codeBlockCopySelectionPlugin.copySelectionPluginKey, 'remove-selection');
77
+ if (dispatch) {
78
+ dispatch(copyToClipboardTr);
79
+ }
80
+ }
81
+ return true;
82
+ };
83
+ var resetCopiedState = exports.resetCopiedState = function resetCopiedState(state, dispatch) {
84
+ var tr = state.tr;
85
+ var codeBlockState = _pluginKey.pluginKey.getState(state);
86
+ var resetCopiedStateTr = tr;
87
+ if (codeBlockState && codeBlockState.contentCopied) {
88
+ resetCopiedStateTr.setMeta(_pluginKey.pluginKey, {
89
+ type: _actions.ACTIONS.SET_COPIED_TO_CLIPBOARD,
90
+ data: false
91
+ });
92
+ resetCopiedStateTr.setMeta(_codeBlockCopySelectionPlugin.copySelectionPluginKey, 'remove-selection');
93
+ if (dispatch) {
94
+ dispatch(resetCopiedStateTr);
95
+ }
96
+ } else {
97
+ var clearSelectionStateTransaction = state.tr;
98
+ clearSelectionStateTransaction.setMeta(_codeBlockCopySelectionPlugin.copySelectionPluginKey, 'remove-selection');
99
+ // note: dispatch should always be defined when called from the
100
+ // floating toolbar. Howver the Command type which floating toolbar uses
101
+ // (and resetCopiedState) uses suggests it's optional.
102
+ if (dispatch) {
103
+ dispatch(clearSelectionStateTransaction);
104
+ }
105
+ }
106
+ return true;
107
+ };
108
+ var ignoreFollowingMutations = exports.ignoreFollowingMutations = function ignoreFollowingMutations(state, dispatch) {
109
+ var tr = state.tr;
110
+ var ignoreFollowingMutationsTr = tr;
111
+ ignoreFollowingMutationsTr.setMeta(_pluginKey.pluginKey, {
112
+ type: _actions.ACTIONS.SET_SHOULD_IGNORE_FOLLOWING_MUTATIONS,
113
+ data: true
114
+ });
115
+ if (dispatch) {
116
+ dispatch(ignoreFollowingMutationsTr);
117
+ }
118
+ return true;
119
+ };
120
+ var resetShouldIgnoreFollowingMutations = exports.resetShouldIgnoreFollowingMutations = function resetShouldIgnoreFollowingMutations(state, dispatch) {
121
+ var tr = state.tr;
122
+ var ignoreFollowingMutationsTr = tr;
123
+ ignoreFollowingMutationsTr.setMeta(_pluginKey.pluginKey, {
124
+ type: _actions.ACTIONS.SET_SHOULD_IGNORE_FOLLOWING_MUTATIONS,
125
+ data: false
126
+ });
127
+ if (dispatch) {
128
+ dispatch(ignoreFollowingMutationsTr);
129
+ }
130
+ return true;
131
+ };
132
+
133
+ /**
134
+ * This function creates a new transaction that inserts a code block,
135
+ * if there is text selected it will wrap the current selection if not it will
136
+ * append the codeblock to the end of the document.
137
+ */
138
+ function createInsertCodeBlockTransaction(_ref) {
139
+ var _state$selection$$fro;
140
+ var state = _ref.state;
141
+ var tr = state.tr;
142
+ var from = state.selection.from;
143
+ var codeBlock = state.schema.nodes.codeBlock;
144
+ var grandParentNodeType = (_state$selection$$fro = state.selection.$from.node(-1)) === null || _state$selection$$fro === void 0 ? void 0 : _state$selection$$fro.type;
145
+ var parentNodeType = state.selection.$from.parent.type;
146
+
147
+ /** We always want to append a codeBlock unless we're inserting into a paragraph
148
+ * AND it's a valid child of the grandparent node.
149
+ * Insert the current selection as codeBlock content unless it contains nodes other
150
+ * than paragraphs and inline.
151
+ */
152
+ var canInsertCodeBlock = (0, _insert.shouldSplitSelectedNodeOnNodeInsertion)({
153
+ parentNodeType: parentNodeType,
154
+ grandParentNodeType: grandParentNodeType,
155
+ content: codeBlock.createAndFill()
156
+ }) && contentAllowedInCodeBlock(state);
157
+ if (canInsertCodeBlock) {
158
+ tr = (0, _transformToCodeBlock.transformToCodeBlockAction)(state, from);
159
+ } else {
160
+ (0, _utils.safeInsert)(codeBlock.createAndFill())(tr).scrollIntoView();
161
+ }
162
+ return tr;
163
+ }
164
+
165
+ /**
166
+ * Check if the current selection contains any nodes that are not permitted
167
+ * as codeBlock child nodes. Note that this allows paragraphs and inline nodes
168
+ * as we extract their text content.
169
+ */
170
+ function contentAllowedInCodeBlock(state) {
171
+ var _state$selection = state.selection,
172
+ $from = _state$selection.$from,
173
+ $to = _state$selection.$to;
174
+ var isAllowedChild = true;
175
+ state.doc.nodesBetween($from.pos, $to.pos, function (node) {
176
+ if (!isAllowedChild) {
177
+ return false;
178
+ }
179
+ return isAllowedChild = node.type === state.schema.nodes.listItem || node.type === state.schema.nodes.bulletList || node.type === state.schema.nodes.orderedList || node.type === state.schema.nodes.paragraph || node.isInline || node.isText;
180
+ });
181
+ return isAllowedChild;
182
+ }
183
+ function insertCodeBlockWithAnalytics(inputMethod, analyticsAPI) {
184
+ return (0, _editorAnalytics.withAnalytics)(analyticsAPI, {
185
+ action: _analytics.ACTION.INSERTED,
186
+ actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
187
+ actionSubjectId: _analytics.ACTION_SUBJECT_ID.CODE_BLOCK,
188
+ attributes: {
189
+ inputMethod: inputMethod
190
+ },
191
+ eventType: _analytics.EVENT_TYPE.TRACK
192
+ })(function (state, dispatch) {
193
+ var tr = createInsertCodeBlockTransaction({
194
+ state: state
195
+ });
196
+ if (dispatch) {
197
+ dispatch(tr);
198
+ }
199
+ return true;
200
+ });
201
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.shouldAutoCloseBracket = exports.getAutoClosingBracketInfo = exports.BRACKET_MAP = void 0;
7
+ var BRACKET_MAP = exports.BRACKET_MAP = {
8
+ '{': '}',
9
+ '[': ']',
10
+ '(': ')'
11
+ };
12
+ var shouldAutoCloseBracket = exports.shouldAutoCloseBracket = function shouldAutoCloseBracket(before, after) {
13
+ // when directly before a closing bracket
14
+ if (/^[}\])]/.test(after)) {
15
+ return true;
16
+ }
17
+
18
+ // exclusion: when directly before a non-whitespace character
19
+ if (/^[^\s]/.test(after)) {
20
+ return false;
21
+ }
22
+ return true;
23
+ };
24
+ var getAutoClosingBracketInfo = exports.getAutoClosingBracketInfo = function getAutoClosingBracketInfo(before, after) {
25
+ var left = Object.keys(BRACKET_MAP).find(function (item) {
26
+ return before.endsWith(item);
27
+ });
28
+ var right = left ? BRACKET_MAP[left] : undefined;
29
+ var hasTrailingMatchingBracket = right ? after.startsWith(right) : false;
30
+ return {
31
+ left: left,
32
+ right: right,
33
+ hasTrailingMatchingBracket: hasTrailingMatchingBracket
34
+ };
35
+ };
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.indent = void 0;
7
+ exports.insertIndent = insertIndent;
8
+ exports.insertNewlineWithIndent = insertNewlineWithIndent;
9
+ exports.outdent = void 0;
10
+ var _analytics = require("@atlaskit/editor-common/analytics");
11
+ var _state = require("@atlaskit/editor-prosemirror/state");
12
+ var _lineHandling = require("./line-handling");
13
+ /**
14
+ * Return the current indentation level
15
+ * @param indentText - Text in the code block that represent an indentation
16
+ * @param indentSize - Size of the indentation token in a string
17
+ */
18
+ function getIndentLevel(indentText, indentSize) {
19
+ if (indentSize === 0 || indentText.length === 0) {
20
+ return 0;
21
+ }
22
+ return indentText.length / indentSize;
23
+ }
24
+ var indent = exports.indent = function indent(editorAnalyticsAPI) {
25
+ return function (state, dispatch) {
26
+ var _getLinesFromSelectio = (0, _lineHandling.getLinesFromSelection)(state),
27
+ text = _getLinesFromSelectio.text,
28
+ start = _getLinesFromSelectio.start;
29
+ var tr = state.tr,
30
+ selection = state.selection;
31
+ (0, _lineHandling.forEachLine)(text, function (line, offset) {
32
+ var _getLineInfo = (0, _lineHandling.getLineInfo)(line),
33
+ indentText = _getLineInfo.indentText,
34
+ indentToken = _getLineInfo.indentToken;
35
+ var indentLevel = getIndentLevel(indentText, indentToken.size);
36
+ var indentToAdd = indentToken.token.repeat(indentToken.size - indentText.length % indentToken.size || indentToken.size);
37
+ tr.insertText(indentToAdd, tr.mapping.map(start + offset, -1));
38
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
39
+ action: _analytics.ACTION.FORMATTED,
40
+ actionSubject: _analytics.ACTION_SUBJECT.TEXT,
41
+ actionSubjectId: _analytics.ACTION_SUBJECT_ID.FORMAT_INDENT,
42
+ eventType: _analytics.EVENT_TYPE.TRACK,
43
+ attributes: {
44
+ inputMethod: _analytics.INPUT_METHOD.KEYBOARD,
45
+ previousIndentationLevel: indentLevel,
46
+ newIndentLevel: indentLevel + 1,
47
+ direction: _analytics.INDENT_DIRECTION.INDENT,
48
+ indentType: _analytics.INDENT_TYPE.CODE_BLOCK
49
+ }
50
+ })(tr);
51
+ if (!selection.empty) {
52
+ tr.setSelection(_state.TextSelection.create(tr.doc, tr.mapping.map(selection.from, -1), tr.selection.to));
53
+ }
54
+ });
55
+ if (dispatch) {
56
+ dispatch(tr);
57
+ }
58
+ return true;
59
+ };
60
+ };
61
+ var outdent = exports.outdent = function outdent(editorAnalyticsAPI) {
62
+ return function (state, dispatch) {
63
+ var _getLinesFromSelectio2 = (0, _lineHandling.getLinesFromSelection)(state),
64
+ text = _getLinesFromSelectio2.text,
65
+ start = _getLinesFromSelectio2.start;
66
+ var tr = state.tr;
67
+ (0, _lineHandling.forEachLine)(text, function (line, offset) {
68
+ var _getLineInfo2 = (0, _lineHandling.getLineInfo)(line),
69
+ indentText = _getLineInfo2.indentText,
70
+ indentToken = _getLineInfo2.indentToken;
71
+ if (indentText) {
72
+ var indentLevel = getIndentLevel(indentText, indentToken.size);
73
+ var unindentLength = indentText.length % indentToken.size || indentToken.size;
74
+ tr.delete(tr.mapping.map(start + offset), tr.mapping.map(start + offset + unindentLength));
75
+ editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
76
+ action: _analytics.ACTION.FORMATTED,
77
+ actionSubject: _analytics.ACTION_SUBJECT.TEXT,
78
+ actionSubjectId: _analytics.ACTION_SUBJECT_ID.FORMAT_INDENT,
79
+ eventType: _analytics.EVENT_TYPE.TRACK,
80
+ attributes: {
81
+ inputMethod: _analytics.INPUT_METHOD.KEYBOARD,
82
+ previousIndentationLevel: indentLevel,
83
+ newIndentLevel: indentLevel - 1,
84
+ direction: _analytics.INDENT_DIRECTION.OUTDENT,
85
+ indentType: _analytics.INDENT_TYPE.CODE_BLOCK
86
+ }
87
+ })(tr);
88
+ }
89
+ });
90
+ if (dispatch) {
91
+ dispatch(tr);
92
+ }
93
+ return true;
94
+ };
95
+ };
96
+ function insertIndent(state, dispatch) {
97
+ var _getStartOfCurrentLin = (0, _lineHandling.getStartOfCurrentLine)(state),
98
+ textAtStartOfLine = _getStartOfCurrentLin.text;
99
+ var _getLineInfo3 = (0, _lineHandling.getLineInfo)(textAtStartOfLine),
100
+ indentToken = _getLineInfo3.indentToken;
101
+ var indentToAdd = indentToken.token.repeat(indentToken.size - textAtStartOfLine.length % indentToken.size || indentToken.size);
102
+ dispatch(state.tr.insertText(indentToAdd));
103
+ return true;
104
+ }
105
+ function insertNewlineWithIndent(state, dispatch) {
106
+ var _getStartOfCurrentLin2 = (0, _lineHandling.getStartOfCurrentLine)(state),
107
+ textAtStartOfLine = _getStartOfCurrentLin2.text;
108
+ var _getLineInfo4 = (0, _lineHandling.getLineInfo)(textAtStartOfLine),
109
+ indentText = _getLineInfo4.indentText;
110
+ if (indentText && dispatch) {
111
+ dispatch(state.tr.insertText('\n' + indentText));
112
+ return true;
113
+ }
114
+ return false;
115
+ }
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getLineInfo = exports.getEndOfCurrentLine = exports.forEachLine = void 0;
7
+ exports.getLinesFromSelection = getLinesFromSelection;
8
+ exports.isSelectionEntirelyInsideCodeBlock = exports.isCursorInsideCodeBlock = exports.getStartOfCurrentLine = void 0;
9
+ var _utils = require("../utils");
10
+ var isSelectionEntirelyInsideCodeBlock = exports.isSelectionEntirelyInsideCodeBlock = function isSelectionEntirelyInsideCodeBlock(state) {
11
+ return state.selection.$from.sameParent(state.selection.$to) && state.selection.$from.parent.type === state.schema.nodes.codeBlock;
12
+ };
13
+ var isCursorInsideCodeBlock = exports.isCursorInsideCodeBlock = function isCursorInsideCodeBlock(state) {
14
+ return !!(0, _utils.getCursor)(state.selection) && isSelectionEntirelyInsideCodeBlock(state);
15
+ };
16
+ var getStartOfCurrentLine = exports.getStartOfCurrentLine = function getStartOfCurrentLine(state) {
17
+ var $from = state.selection.$from;
18
+ if ($from.nodeBefore && $from.nodeBefore.isText) {
19
+ var prevNewLineIndex = $from.nodeBefore.text.lastIndexOf('\n');
20
+ return {
21
+ text: $from.nodeBefore.text.substring(prevNewLineIndex + 1),
22
+ pos: $from.start() + prevNewLineIndex + 1
23
+ };
24
+ }
25
+ return {
26
+ text: '',
27
+ pos: $from.pos
28
+ };
29
+ };
30
+ var getEndOfCurrentLine = exports.getEndOfCurrentLine = function getEndOfCurrentLine(state) {
31
+ var $to = state.selection.$to;
32
+ if ($to.nodeAfter && $to.nodeAfter.isText) {
33
+ var nextNewLineIndex = $to.nodeAfter.text.indexOf('\n');
34
+ return {
35
+ text: $to.nodeAfter.text.substring(0, nextNewLineIndex >= 0 ? nextNewLineIndex : undefined),
36
+ pos: nextNewLineIndex >= 0 ? $to.pos + nextNewLineIndex : $to.end()
37
+ };
38
+ }
39
+ return {
40
+ text: '',
41
+ pos: $to.pos
42
+ };
43
+ };
44
+ function getLinesFromSelection(state) {
45
+ var _getStartOfCurrentLin = getStartOfCurrentLine(state),
46
+ start = _getStartOfCurrentLin.pos;
47
+ var _getEndOfCurrentLine = getEndOfCurrentLine(state),
48
+ end = _getEndOfCurrentLine.pos;
49
+ var text = state.doc.textBetween(start, end);
50
+ return {
51
+ text: text,
52
+ start: start,
53
+ end: end
54
+ };
55
+ }
56
+ var forEachLine = exports.forEachLine = function forEachLine(text, callback) {
57
+ var offset = 0;
58
+ text.split('\n').forEach(function (line) {
59
+ callback(line, offset);
60
+ offset += line.length + 1;
61
+ });
62
+ };
63
+ var SPACE = {
64
+ token: ' ',
65
+ size: 2,
66
+ regex: /[^ ]/
67
+ };
68
+ var TAB = {
69
+ token: '\t',
70
+ size: 1,
71
+ regex: /[^\t]/
72
+ };
73
+ var getLineInfo = exports.getLineInfo = function getLineInfo(line) {
74
+ var indentToken = line.startsWith('\t') ? TAB : SPACE;
75
+ var indentLength = line.search(indentToken.regex);
76
+ var indentText = line.substring(0, indentLength >= 0 ? indentLength : line.length);
77
+ return {
78
+ indentToken: indentToken,
79
+ indentText: indentText
80
+ };
81
+ };
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.isCursorBeforeClosingCharacter = exports.isClosingCharacter = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _bracketHandling = require("./bracket-handling");
10
+ var _quoteHandling = require("./quote-handling");
11
+ 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; }
12
+ 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; }
13
+ var PAIRED_CHARACTER_MAP = _objectSpread(_objectSpread({}, _bracketHandling.BRACKET_MAP), _quoteHandling.QUOTE_MAP);
14
+ var isCursorBeforeClosingCharacter = exports.isCursorBeforeClosingCharacter = function isCursorBeforeClosingCharacter(after) {
15
+ return Object.keys(PAIRED_CHARACTER_MAP).some(function (leftCharacter) {
16
+ return after.startsWith(PAIRED_CHARACTER_MAP[leftCharacter]);
17
+ });
18
+ };
19
+ var isClosingCharacter = exports.isClosingCharacter = function isClosingCharacter(text) {
20
+ return Object.keys(PAIRED_CHARACTER_MAP).some(function (leftCharacter) {
21
+ return text === PAIRED_CHARACTER_MAP[leftCharacter];
22
+ });
23
+ };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.shouldAutoCloseQuote = exports.getAutoClosingQuoteInfo = exports.QUOTE_MAP = void 0;
7
+ var QUOTE_MAP = exports.QUOTE_MAP = {
8
+ "'": "'",
9
+ '"': '"',
10
+ '`': '`'
11
+ };
12
+ var shouldAutoCloseQuote = exports.shouldAutoCloseQuote = function shouldAutoCloseQuote(before, after) {
13
+ // when directly before a closing bracket
14
+ if (/^[}\])]/.test(after)) {
15
+ return true;
16
+ }
17
+
18
+ // exclusion: when directly before a non-whitespace character
19
+ if (/^[^\s]/.test(after)) {
20
+ return false;
21
+ }
22
+
23
+ // exclusion: when directly after a letter or quote
24
+ if (/[A-Za-z0-9]$/.test(before) || /[\'\"\`]$/.test(before)) {
25
+ return false;
26
+ }
27
+ return true;
28
+ };
29
+ var getAutoClosingQuoteInfo = exports.getAutoClosingQuoteInfo = function getAutoClosingQuoteInfo(before, after) {
30
+ var left = Object.keys(QUOTE_MAP).find(function (item) {
31
+ return before.endsWith(item);
32
+ });
33
+ var right = left ? QUOTE_MAP[left] : undefined;
34
+ var hasTrailingMatchingQuote = right ? after.startsWith(right) : false;
35
+ return {
36
+ left: left,
37
+ right: right,
38
+ hasTrailingMatchingQuote: hasTrailingMatchingQuote
39
+ };
40
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ Object.defineProperty(exports, "codeBlockPlugin", {
8
+ enumerable: true,
9
+ get: function get() {
10
+ return _plugin.default;
11
+ }
12
+ });
13
+ var _plugin = _interopRequireDefault(require("./plugin"));
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.DEFAULT_LANGUAGES = void 0;
8
+ exports.createLanguageList = createLanguageList;
9
+ exports.filterSupportedLanguages = filterSupportedLanguages;
10
+ exports.findMatchedLanguage = findMatchedLanguage;
11
+ exports.getLanguageIdentifier = getLanguageIdentifier;
12
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
+ var _constants = require("@atlaskit/code/constants");
14
+ // We expect alias[0] to be used for the ADF attribute, see ED-2813
15
+ var DEFAULT_LANGUAGES = exports.DEFAULT_LANGUAGES = [{
16
+ name: '(None)',
17
+ alias: ['none'],
18
+ value: 'none'
19
+ }].concat((0, _toConsumableArray2.default)(_constants.SUPPORTED_LANGUAGES));
20
+ function findMatchedLanguage(supportedLanguages, language) {
21
+ if (!language) {
22
+ return undefined;
23
+ }
24
+ var matches = supportedLanguages.filter(function (supportedLanguage) {
25
+ return supportedLanguage.alias.indexOf(language.toLowerCase()) !== -1;
26
+ });
27
+ if (matches.length > 0) {
28
+ return matches[0];
29
+ }
30
+ return undefined;
31
+ }
32
+ function filterSupportedLanguages(supportedLanguages) {
33
+ if (!supportedLanguages || !supportedLanguages.length) {
34
+ return DEFAULT_LANGUAGES;
35
+ }
36
+ return DEFAULT_LANGUAGES.filter(function (language) {
37
+ var i = language.alias.length;
38
+ while (i--) {
39
+ if (supportedLanguages.indexOf(language.alias[i]) > -1) {
40
+ return true;
41
+ }
42
+ }
43
+ return false;
44
+ });
45
+ }
46
+ function getLanguageIdentifier(language) {
47
+ return language.alias[0];
48
+ }
49
+ function createLanguageList(supportedLanguages) {
50
+ return supportedLanguages.sort(function (left, right) {
51
+ if (left.alias[0] === 'none') {
52
+ return -1;
53
+ }
54
+ if (left.name.toLowerCase() > right.name.toLowerCase()) {
55
+ return 1;
56
+ }
57
+ if (left.name.toLowerCase() < right.name.toLowerCase()) {
58
+ return -1;
59
+ }
60
+ return 0;
61
+ });
62
+ }