@contentful/field-editor-markdown 1.2.1 → 1.3.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 (136) hide show
  1. package/dist/cjs/MarkdownActions.js +235 -0
  2. package/dist/cjs/MarkdownEditor.js +180 -0
  3. package/dist/cjs/__fixtures__/FakeSdk.js +183 -0
  4. package/dist/cjs/__fixtures__/asset/index.js +37 -0
  5. package/dist/cjs/__fixtures__/content-type/index.js +16 -0
  6. package/dist/cjs/__fixtures__/entry/index.js +33 -0
  7. package/dist/cjs/__fixtures__/fixtures.js +71 -0
  8. package/dist/cjs/__fixtures__/locale/index.js +40 -0
  9. package/dist/cjs/__fixtures__/space/index.js +16 -0
  10. package/dist/cjs/codemirrorImports.js +9 -0
  11. package/dist/cjs/components/HeadingSelector.js +66 -0
  12. package/dist/cjs/components/InsertLinkSelector.js +86 -0
  13. package/dist/cjs/components/MarkdownBottomBar.js +111 -0
  14. package/dist/cjs/components/MarkdownConstraints.js +79 -0
  15. package/dist/cjs/components/MarkdownPreview.js +249 -0
  16. package/dist/cjs/components/MarkdownTabs.js +128 -0
  17. package/dist/cjs/components/MarkdownTextarea/CodeMirrorWrapper.js +383 -0
  18. package/dist/cjs/components/MarkdownTextarea/MarkdownCommands.js +233 -0
  19. package/dist/cjs/components/MarkdownTextarea/MarkdownTextarea.js +190 -0
  20. package/dist/cjs/components/MarkdownTextarea/createMarkdownEditor.js +101 -0
  21. package/dist/cjs/components/MarkdownToolbar.js +367 -0
  22. package/dist/cjs/components/icons.js +193 -0
  23. package/dist/cjs/dialogs/CheatsheetModalDialog.js +239 -0
  24. package/dist/cjs/dialogs/ConfirmInsertAssetModalDialog.js +94 -0
  25. package/dist/cjs/dialogs/EmdebExternalContentDialog.js +202 -0
  26. package/dist/cjs/dialogs/InsertLinkModalDialog.js +149 -0
  27. package/dist/cjs/dialogs/InsertTableModalDialog.js +140 -0
  28. package/dist/cjs/dialogs/SpecialCharacterModalDialog.js +146 -0
  29. package/dist/cjs/dialogs/ZenModeModalDialog.js +257 -0
  30. package/dist/cjs/dialogs/openMarkdownDialog.js +121 -0
  31. package/dist/cjs/dialogs/renderMarkdownDialog.js +108 -0
  32. package/dist/cjs/index.js +29 -0
  33. package/dist/cjs/types.js +20 -0
  34. package/dist/cjs/utils/insertAssetLinks.js +122 -0
  35. package/dist/cjs/utils/insertAssetLinks.spec.js +22 -0
  36. package/dist/cjs/utils/isValidUrl.js +22 -0
  37. package/dist/cjs/utils/linkOrganizer.js +187 -0
  38. package/dist/cjs/utils/linkOrganizer.spec.js +96 -0
  39. package/dist/cjs/utils/replaceMailtoAmp.js +15 -0
  40. package/dist/cjs/utils/replaceMailtoAmp.spec.js +22 -0
  41. package/dist/cjs/utils/specialCharacters.js +228 -0
  42. package/dist/cjs/utils/userAgent.js +28 -0
  43. package/dist/esm/MarkdownActions.js +186 -0
  44. package/dist/esm/MarkdownEditor.js +118 -0
  45. package/dist/esm/__fixtures__/FakeSdk.js +173 -0
  46. package/dist/esm/__fixtures__/asset/index.js +6 -0
  47. package/dist/esm/__fixtures__/content-type/index.js +2 -0
  48. package/dist/esm/__fixtures__/entry/index.js +5 -0
  49. package/dist/esm/__fixtures__/fixtures.js +6 -0
  50. package/dist/esm/__fixtures__/locale/index.js +15 -0
  51. package/dist/esm/__fixtures__/space/index.js +2 -0
  52. package/dist/esm/codemirrorImports.js +5 -0
  53. package/dist/esm/components/HeadingSelector.js +17 -0
  54. package/dist/esm/components/InsertLinkSelector.js +37 -0
  55. package/dist/esm/components/MarkdownBottomBar.js +46 -0
  56. package/dist/esm/components/MarkdownConstraints.js +25 -0
  57. package/dist/esm/components/MarkdownPreview.js +195 -0
  58. package/dist/esm/components/MarkdownTabs.js +74 -0
  59. package/dist/esm/components/MarkdownTextarea/CodeMirrorWrapper.js +329 -0
  60. package/dist/esm/components/MarkdownTextarea/MarkdownCommands.js +218 -0
  61. package/dist/esm/components/MarkdownTextarea/MarkdownTextarea.js +136 -0
  62. package/dist/esm/components/MarkdownTextarea/createMarkdownEditor.js +52 -0
  63. package/dist/esm/components/MarkdownToolbar.js +302 -0
  64. package/dist/esm/components/icons.js +112 -0
  65. package/dist/esm/dialogs/CheatsheetModalDialog.js +177 -0
  66. package/dist/esm/dialogs/ConfirmInsertAssetModalDialog.js +37 -0
  67. package/dist/esm/dialogs/EmdebExternalContentDialog.js +140 -0
  68. package/dist/esm/dialogs/InsertLinkModalDialog.js +92 -0
  69. package/dist/esm/dialogs/InsertTableModalDialog.js +78 -0
  70. package/dist/esm/dialogs/SpecialCharacterModalDialog.js +84 -0
  71. package/dist/esm/dialogs/ZenModeModalDialog.js +195 -0
  72. package/dist/esm/dialogs/openMarkdownDialog.js +72 -0
  73. package/dist/esm/dialogs/renderMarkdownDialog.js +59 -0
  74. package/dist/esm/index.js +5 -0
  75. package/dist/esm/types.js +10 -0
  76. package/dist/esm/utils/insertAssetLinks.js +99 -0
  77. package/dist/esm/utils/insertAssetLinks.spec.js +18 -0
  78. package/dist/esm/utils/isValidUrl.js +4 -0
  79. package/dist/esm/utils/linkOrganizer.js +152 -0
  80. package/dist/esm/utils/linkOrganizer.spec.js +53 -0
  81. package/dist/esm/utils/replaceMailtoAmp.js +5 -0
  82. package/dist/esm/utils/replaceMailtoAmp.spec.js +18 -0
  83. package/dist/esm/utils/specialCharacters.js +218 -0
  84. package/dist/esm/utils/userAgent.js +13 -0
  85. package/dist/{MarkdownActions.d.ts → types/MarkdownActions.d.ts} +38 -38
  86. package/dist/{MarkdownEditor.d.ts → types/MarkdownEditor.d.ts} +22 -22
  87. package/dist/types/__fixtures__/FakeSdk.d.ts +8 -0
  88. package/dist/types/__fixtures__/asset/index.d.ts +6 -0
  89. package/dist/types/__fixtures__/content-type/index.d.ts +2 -0
  90. package/dist/types/__fixtures__/entry/index.d.ts +5 -0
  91. package/dist/types/__fixtures__/fixtures.d.ts +6 -0
  92. package/dist/types/__fixtures__/locale/index.d.ts +42 -0
  93. package/dist/types/__fixtures__/space/index.d.ts +2 -0
  94. package/dist/{codemirrorImports.d.ts → types/codemirrorImports.d.ts} +5 -5
  95. package/dist/{components → types/components}/HeadingSelector.d.ts +7 -7
  96. package/dist/{components → types/components}/InsertLinkSelector.d.ts +9 -9
  97. package/dist/{components → types/components}/MarkdownBottomBar.d.ts +11 -11
  98. package/dist/{components → types/components}/MarkdownConstraints.d.ts +6 -6
  99. package/dist/{components → types/components}/MarkdownPreview.d.ts +14 -14
  100. package/dist/{components → types/components}/MarkdownTabs.d.ts +8 -8
  101. package/dist/{components → types/components}/MarkdownTextarea/CodeMirrorWrapper.d.ts +58 -58
  102. package/dist/{components → types/components}/MarkdownTextarea/MarkdownCommands.d.ts +33 -33
  103. package/dist/{components → types/components}/MarkdownTextarea/MarkdownTextarea.d.ts +17 -17
  104. package/dist/{components → types/components}/MarkdownTextarea/createMarkdownEditor.d.ts +55 -55
  105. package/dist/{components → types/components}/MarkdownToolbar.d.ts +12 -12
  106. package/dist/types/components/icons.d.ts +18 -0
  107. package/dist/{dialogs → types/dialogs}/CheatsheetModalDialog.d.ts +4 -4
  108. package/dist/{dialogs → types/dialogs}/ConfirmInsertAssetModalDialog.d.ts +23 -23
  109. package/dist/{dialogs → types/dialogs}/EmdebExternalContentDialog.d.ts +9 -9
  110. package/dist/{dialogs → types/dialogs}/InsertLinkModalDialog.d.ts +17 -17
  111. package/dist/{dialogs → types/dialogs}/InsertTableModalDialog.d.ts +13 -13
  112. package/dist/{dialogs → types/dialogs}/SpecialCharacterModalDialog.d.ts +9 -9
  113. package/dist/{dialogs → types/dialogs}/ZenModeModalDialog.d.ts +24 -24
  114. package/dist/{dialogs → types/dialogs}/openMarkdownDialog.d.ts +5 -5
  115. package/dist/{dialogs → types/dialogs}/renderMarkdownDialog.d.ts +8 -8
  116. package/dist/{index.d.ts → types/index.d.ts} +5 -5
  117. package/dist/{types.d.ts → types/types.d.ts} +75 -75
  118. package/dist/{utils → types/utils}/insertAssetLinks.d.ts +29 -29
  119. package/dist/types/utils/insertAssetLinks.spec.d.ts +1 -0
  120. package/dist/{utils → types/utils}/isValidUrl.d.ts +2 -2
  121. package/dist/{utils → types/utils}/linkOrganizer.d.ts +6 -6
  122. package/dist/types/utils/linkOrganizer.spec.d.ts +1 -0
  123. package/dist/{utils → types/utils}/replaceMailtoAmp.d.ts +1 -1
  124. package/dist/types/utils/replaceMailtoAmp.spec.d.ts +1 -0
  125. package/dist/{utils → types/utils}/specialCharacters.d.ts +4 -4
  126. package/dist/{utils → types/utils}/userAgent.d.ts +1 -1
  127. package/package.json +25 -11
  128. package/CHANGELOG.md +0 -314
  129. package/dist/components/icons.d.ts +0 -18
  130. package/dist/field-editor-markdown.cjs.development.js +0 -3609
  131. package/dist/field-editor-markdown.cjs.development.js.map +0 -1
  132. package/dist/field-editor-markdown.cjs.production.min.js +0 -216
  133. package/dist/field-editor-markdown.cjs.production.min.js.map +0 -1
  134. package/dist/field-editor-markdown.esm.js +0 -3599
  135. package/dist/field-editor-markdown.esm.js.map +0 -1
  136. package/dist/index.js +0 -8
@@ -0,0 +1,383 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "create", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return create;
9
+ }
10
+ });
11
+ const _codemirror = _interop_require_default(require("codemirror"));
12
+ require("codemirror/addon/edit/matchbrackets");
13
+ const _throttle = _interop_require_default(require("lodash/throttle"));
14
+ const _transform = _interop_require_default(require("lodash/transform"));
15
+ const _userAgent = _interop_require_wildcard(require("../../utils/userAgent"));
16
+ function _interop_require_default(obj) {
17
+ return obj && obj.__esModule ? obj : {
18
+ default: obj
19
+ };
20
+ }
21
+ function _getRequireWildcardCache(nodeInterop) {
22
+ if (typeof WeakMap !== "function") return null;
23
+ var cacheBabelInterop = new WeakMap();
24
+ var cacheNodeInterop = new WeakMap();
25
+ return (_getRequireWildcardCache = function(nodeInterop) {
26
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
27
+ })(nodeInterop);
28
+ }
29
+ function _interop_require_wildcard(obj, nodeInterop) {
30
+ if (!nodeInterop && obj && obj.__esModule) {
31
+ return obj;
32
+ }
33
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
34
+ return {
35
+ default: obj
36
+ };
37
+ }
38
+ var cache = _getRequireWildcardCache(nodeInterop);
39
+ if (cache && cache.has(obj)) {
40
+ return cache.get(obj);
41
+ }
42
+ var newObj = {};
43
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
44
+ for(var key in obj){
45
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
46
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
47
+ if (desc && (desc.get || desc.set)) {
48
+ Object.defineProperty(newObj, key, desc);
49
+ } else {
50
+ newObj[key] = obj[key];
51
+ }
52
+ }
53
+ }
54
+ newObj.default = obj;
55
+ if (cache) {
56
+ cache.set(obj, newObj);
57
+ }
58
+ return newObj;
59
+ }
60
+ function stripUnit(value) {
61
+ if (typeof value !== 'string') return value;
62
+ const cssRegex = /^([+-]?(?:\d+|\d*\.\d+))([a-z]*|%)$/;
63
+ const matchedValue = value.match(cssRegex);
64
+ if (!matchedValue) {
65
+ throw new Error("Couldn't match unit in given string");
66
+ }
67
+ return parseFloat(value);
68
+ }
69
+ function create(host, options) {
70
+ const { direction , fixedHeight , height , readOnly } = options || {};
71
+ let initializedWithValue = false;
72
+ const LF = '\n';
73
+ const EDITOR_SIZE = {
74
+ min: height ? stripUnit(height) : 300,
75
+ max: 500,
76
+ shift: 50
77
+ };
78
+ const cm = (0, _codemirror.default)(host, {
79
+ direction,
80
+ readOnly,
81
+ mode: 'markdown',
82
+ lineNumbers: false,
83
+ undoDepth: 200,
84
+ matchBrackets: true,
85
+ lineWrapping: true,
86
+ lineSeparator: null,
87
+ theme: 'elegant',
88
+ tabSize: 2,
89
+ indentWithTabs: false,
90
+ indentUnit: 2,
91
+ autoRefresh: true,
92
+ spellcheck: true,
93
+ inputStyle: 'contenteditable'
94
+ });
95
+ cm.setSize('100%', EDITOR_SIZE.min);
96
+ if (!fixedHeight) {
97
+ cm.on('change', (0, _throttle.default)(assureHeight, 150));
98
+ }
99
+ cm.setOption('extraKeys', {
100
+ Tab: function() {
101
+ replaceSelectedText(getIndentation());
102
+ },
103
+ Enter: 'newlineAndIndentContinueMarkdownList',
104
+ Esc: ()=>{
105
+ cm.getInputField().blur();
106
+ }
107
+ });
108
+ return {
109
+ destroy,
110
+ disable,
111
+ enable,
112
+ attachEvent,
113
+ addKeyShortcuts,
114
+ setValue,
115
+ cmd,
116
+ moveToLineBeginning,
117
+ moveIfNotEmpty,
118
+ restoreCursor,
119
+ moveToLineEnd,
120
+ usePrimarySelection,
121
+ focus,
122
+ select,
123
+ selectBackwards,
124
+ selectAll: ()=>cm.execCommand('selectAll'),
125
+ extendSelectionBy,
126
+ insertAtCursor,
127
+ insertAtLineBeginning,
128
+ wrapSelection,
129
+ removeFromLineBeginning,
130
+ removeSelectedText,
131
+ replaceSelectedText,
132
+ getCursor,
133
+ setCursor,
134
+ getSelection,
135
+ getLine,
136
+ isLineEmpty,
137
+ getSelectedText,
138
+ getSelectionLength,
139
+ getCurrentLine,
140
+ getCurrentLineNumber,
141
+ getCurrentCharacter,
142
+ getCurrentLineLength,
143
+ lineStartsWith,
144
+ getIndentation,
145
+ getNl,
146
+ getValue,
147
+ getLinesCount,
148
+ getHistorySize,
149
+ setReadOnly: (value)=>cm.setOption('readOnly', value),
150
+ getHistory: ()=>cm.getHistory(),
151
+ setHistory: (history)=>cm.setHistory(history),
152
+ setFullsize: ()=>{
153
+ cm.setSize('100%', '100%');
154
+ cm.refresh();
155
+ },
156
+ refresh: ()=>cm.refresh()
157
+ };
158
+ function destroy() {
159
+ cm.toTextArea();
160
+ }
161
+ function disable() {
162
+ cm.setOption('readOnly', 'nocursor');
163
+ }
164
+ function enable() {
165
+ cm.setOption('readOnly', false);
166
+ }
167
+ function assureHeight() {
168
+ const current = cm.heightAtLine(cm.lastLine(), 'local') + EDITOR_SIZE.shift;
169
+ let next = current;
170
+ if (current < EDITOR_SIZE.min) {
171
+ next = EDITOR_SIZE.min;
172
+ }
173
+ if (current > EDITOR_SIZE.max) {
174
+ next = EDITOR_SIZE.max;
175
+ }
176
+ cm.setSize('100%', next);
177
+ }
178
+ function attachEvent(name, fn, throttleInterval) {
179
+ if (throttleInterval) {
180
+ fn = (0, _throttle.default)(fn, throttleInterval);
181
+ }
182
+ cm.on(name, fn);
183
+ }
184
+ function addKeyShortcuts(map) {
185
+ const ctrlKey = _userAgent.getCtrlKey();
186
+ cm.addKeyMap((0, _transform.default)(map, (acc, value, key)=>{
187
+ acc[ctrlKey + '-' + key] = value;
188
+ }, {}));
189
+ }
190
+ function setValue(value) {
191
+ value = value || '';
192
+ if (getValue() === value) {
193
+ return;
194
+ }
195
+ const line = getCurrentLineNumber();
196
+ const ch = getCurrentCharacter();
197
+ cm.setValue(value);
198
+ restoreCursor(ch, line, true);
199
+ if (!initializedWithValue) {
200
+ cm.clearHistory();
201
+ initializedWithValue = true;
202
+ }
203
+ }
204
+ function cmd(name) {
205
+ cm.execCommand(name);
206
+ cm.focus();
207
+ }
208
+ function moveToLineBeginning(lineNumber) {
209
+ cm.setCursor({
210
+ line: defaultToCurrentLineNumber(lineNumber),
211
+ ch: 0
212
+ });
213
+ cm.focus();
214
+ }
215
+ function moveIfNotEmpty() {
216
+ if (getCurrentLineLength() < 1) {
217
+ return;
218
+ }
219
+ const next = getCurrentLineNumber() + 1;
220
+ if (cm.lastLine() < next) {
221
+ moveToLineEnd();
222
+ insertAtCursor(getNl());
223
+ }
224
+ moveToLineBeginning(next);
225
+ }
226
+ function restoreCursor(character, lineNumber, noFocus) {
227
+ cm.setCursor(defaultToCurrentLineNumber(lineNumber), character, {
228
+ scroll: !noFocus
229
+ });
230
+ if (!noFocus) {
231
+ cm.focus();
232
+ }
233
+ }
234
+ function moveToLineEnd(lineNumber) {
235
+ cm.setCursor({
236
+ line: defaultToCurrentLineNumber(lineNumber),
237
+ ch: getCurrentLineLength()
238
+ });
239
+ cm.focus();
240
+ }
241
+ function defaultToCurrentLineNumber(lineNumber) {
242
+ if (lineNumber === 0 || lineNumber !== undefined && lineNumber > 0) {
243
+ return lineNumber;
244
+ }
245
+ return getCurrentLineNumber();
246
+ }
247
+ function usePrimarySelection() {
248
+ cmd('singleSelection');
249
+ }
250
+ function focus() {
251
+ cm.focus();
252
+ }
253
+ function select(from, to) {
254
+ cm.setSelection(from, to);
255
+ cm.focus();
256
+ }
257
+ function selectBackwards(skip, len) {
258
+ select(getPos(-skip - len), getPos(-skip));
259
+ function getPos(modifier) {
260
+ return {
261
+ line: getCurrentLineNumber(),
262
+ ch: getCurrentCharacter() + modifier
263
+ };
264
+ }
265
+ }
266
+ function extendSelectionBy(modifier) {
267
+ select(getPos('anchor', 0), getPos('head', modifier));
268
+ function getPos(prop, modifier) {
269
+ const selection = getSelection();
270
+ if (!selection) {
271
+ return {
272
+ line: 0,
273
+ ch: 0
274
+ };
275
+ }
276
+ return {
277
+ line: selection[prop].line,
278
+ ch: selection[prop].ch + modifier
279
+ };
280
+ }
281
+ }
282
+ function insertAtCursor(text) {
283
+ cm.replaceRange(text, cm.getCursor());
284
+ cm.focus();
285
+ }
286
+ function insertAtLineBeginning(text) {
287
+ const initialCh = getCurrentCharacter();
288
+ moveToLineBeginning();
289
+ insertAtCursor(text);
290
+ restoreCursor(initialCh + text.length);
291
+ cm.focus();
292
+ }
293
+ function wrapSelection(wrapper) {
294
+ const replacement = wrapper + getSelectedText() + wrapper;
295
+ const selection = getSelection();
296
+ if (selection) {
297
+ cm.replaceRange(replacement, selection.anchor, selection?.head);
298
+ cm.focus();
299
+ }
300
+ }
301
+ function removeFromLineBeginning(charCount) {
302
+ const lineNumber = getCurrentLineNumber();
303
+ cm.replaceRange('', {
304
+ line: lineNumber,
305
+ ch: 0
306
+ }, {
307
+ line: lineNumber,
308
+ ch: charCount
309
+ });
310
+ cm.focus();
311
+ }
312
+ function removeSelectedText() {
313
+ cm.replaceSelection('');
314
+ cm.focus();
315
+ }
316
+ function replaceSelectedText(replacement, select) {
317
+ cm.replaceSelection(replacement, select);
318
+ cm.focus();
319
+ }
320
+ function getCursor() {
321
+ return cm.getCursor();
322
+ }
323
+ function setCursor(cursor) {
324
+ cm.setCursor(cursor);
325
+ }
326
+ function getSelection() {
327
+ const selections = cm.listSelections();
328
+ if (!cm.somethingSelected() || !selections || selections.length < 1) {
329
+ return null;
330
+ }
331
+ return selections[0];
332
+ }
333
+ function getLine(lineNumber) {
334
+ return cm.getLine(lineNumber) || '';
335
+ }
336
+ function isLineEmpty(lineNumber) {
337
+ const n = defaultToCurrentLineNumber(lineNumber);
338
+ return n > -1 && getLine(n).length < 1 && n < cm.lineCount();
339
+ }
340
+ function getLinesCount() {
341
+ return cm.lineCount();
342
+ }
343
+ function getSelectedText() {
344
+ return getSelection() ? cm.getSelection() : '';
345
+ }
346
+ function getSelectionLength() {
347
+ return getSelectedText().length;
348
+ }
349
+ function getCurrentLine() {
350
+ return getLine(getCurrentLineNumber());
351
+ }
352
+ function getCurrentLineNumber() {
353
+ return cm.getCursor().line;
354
+ }
355
+ function getCurrentCharacter() {
356
+ return cm.getCursor().ch;
357
+ }
358
+ function getCurrentLineLength() {
359
+ return getCurrentLine().length;
360
+ }
361
+ function lineStartsWith(text) {
362
+ return getCurrentLine().startsWith(text);
363
+ }
364
+ function getIndentation() {
365
+ return repeat(' ', cm.getOption('indentUnit') ?? 0);
366
+ }
367
+ function getNl(n = 1) {
368
+ if (n < 1) {
369
+ return '';
370
+ }
371
+ return repeat(LF, n);
372
+ }
373
+ function getValue() {
374
+ return cm.getValue() || '';
375
+ }
376
+ function getHistorySize(which) {
377
+ const history = cm.historySize();
378
+ return which ? history[which] : history;
379
+ }
380
+ function repeat(what, n) {
381
+ return new Array(n + 1).join(what);
382
+ }
383
+ }
@@ -0,0 +1,233 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "create", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return create;
9
+ }
10
+ });
11
+ const _max = _interop_require_default(require("lodash/max"));
12
+ const _min = _interop_require_default(require("lodash/min"));
13
+ const _range = _interop_require_default(require("lodash/range"));
14
+ const _times = _interop_require_default(require("lodash/times"));
15
+ const _repeat = _interop_require_default(require("lodash/repeat"));
16
+ function _interop_require_default(obj) {
17
+ return obj && obj.__esModule ? obj : {
18
+ default: obj
19
+ };
20
+ }
21
+ function createPrefixToggleFn(prefix) {
22
+ return (editor)=>{
23
+ if (editor.lineStartsWith(prefix)) {
24
+ editor.removeFromLineBeginning(prefix.length);
25
+ } else {
26
+ editor.insertAtLineBeginning(prefix);
27
+ }
28
+ };
29
+ }
30
+ function wrapSelection(editor, marker, emptyText) {
31
+ return ()=>{
32
+ editor.usePrimarySelection();
33
+ if (editor.getSelection()) {
34
+ const selectedText = editor.getSelectedText();
35
+ if (selectedText.startsWith(marker) && selectedText.endsWith(marker)) {
36
+ const markerLength = marker.length;
37
+ const textWithoutMarker = selectedText.slice(markerLength, selectedText.length - markerLength);
38
+ editor.replaceSelectedText(textWithoutMarker);
39
+ } else {
40
+ editor.wrapSelection(marker);
41
+ }
42
+ } else {
43
+ editor.insertAtCursor(marker + emptyText + marker);
44
+ editor.selectBackwards(marker.length, emptyText.length);
45
+ }
46
+ };
47
+ }
48
+ const HEADER_CHAR = '#';
49
+ const quoteToggleFn = createPrefixToggleFn('> ');
50
+ const codeToggleFn = createPrefixToggleFn(' ');
51
+ function create(editor) {
52
+ return {
53
+ bold: wrapSelection(editor, '__', 'text in bold'),
54
+ italic: wrapSelection(editor, '*', 'text in italic'),
55
+ strike: wrapSelection(editor, '~~', 'striked out'),
56
+ quote: modifySelection(editor, quoteToggleFn),
57
+ code: modifySelection(editor, codeToggleFn),
58
+ link,
59
+ h1: toggleHeader(editor, 1),
60
+ h2: toggleHeader(editor, 2),
61
+ h3: toggleHeader(editor, 3),
62
+ ul: modifySelection(editor, ulToggleFn, true),
63
+ ol: modifySelection(editor, olToggleFn, true),
64
+ undo: function() {
65
+ editor.cmd('undo');
66
+ },
67
+ redo: function() {
68
+ editor.cmd('redo');
69
+ },
70
+ hr,
71
+ indent,
72
+ dedent,
73
+ table
74
+ };
75
+ function hr() {
76
+ editor.moveIfNotEmpty();
77
+ const nl = editor.getNl();
78
+ const markup = nl + '---' + nl + nl;
79
+ editor.insertAtCursor(markup);
80
+ }
81
+ function indent() {
82
+ editor.insertAtLineBeginning(editor.getIndentation());
83
+ }
84
+ function dedent() {
85
+ const indentation = editor.getIndentation();
86
+ if (editor.lineStartsWith(indentation)) {
87
+ editor.removeFromLineBeginning(indentation.length);
88
+ }
89
+ }
90
+ function table(config) {
91
+ const nl = editor.getNl();
92
+ editor.moveIfNotEmpty();
93
+ editor.insertAtCursor(nl);
94
+ const line = editor.getCurrentLineNumber();
95
+ editor.insertAtCursor(tableTemplate(config.rows, config.cols).join(nl));
96
+ editor.insertAtCursor(nl + nl);
97
+ editor.restoreCursor(2, line);
98
+ }
99
+ function link(url, text, title) {
100
+ editor.usePrimarySelection();
101
+ const linkTitle = title ? ' "' + title + '"' : '';
102
+ const link = text ? '[' + text + '](' + url + linkTitle + ')' : '<' + url + '>';
103
+ editor.replaceSelectedText(link, 'around');
104
+ }
105
+ }
106
+ function modifySelection(editor, toggleFn, isList) {
107
+ return ()=>{
108
+ editor.usePrimarySelection();
109
+ const selection = editor.getSelection();
110
+ if (selection) {
111
+ forLineIn(selection, (lineNumber, listNumber)=>{
112
+ editor.moveToLineBeginning(lineNumber);
113
+ toggleFn(editor, listNumber);
114
+ });
115
+ editor.moveToLineEnd();
116
+ } else {
117
+ if (isList && !getListNumber(editor) && !editor.lineStartsWith('- ')) {
118
+ prepareListWhitespace(editor);
119
+ }
120
+ toggleFn(editor);
121
+ }
122
+ };
123
+ }
124
+ function forLineIn(selection, cb) {
125
+ const lines = [
126
+ selection.anchor.line,
127
+ selection.head.line
128
+ ];
129
+ const maxNumber = (0, _max.default)(lines);
130
+ const minNumber = (0, _min.default)(lines);
131
+ const lineRange = (0, _range.default)(minNumber || 0, maxNumber !== undefined ? maxNumber + 1 : undefined);
132
+ lineRange.forEach((lineNumber, i)=>{
133
+ cb(lineNumber, i + 1);
134
+ });
135
+ }
136
+ function prepareListWhitespace(editor) {
137
+ const line = editor.getCurrentLineNumber();
138
+ const isCurrentLineEmpty = editor.isLineEmpty(line);
139
+ const isPrevLineEmpty = line > 0 ? editor.isLineEmpty(line - 1) : false;
140
+ const isNextLineEmpty = line < editor.getLinesCount() - 1 ? editor.isLineEmpty(line + 1) : true;
141
+ let linesToInsert = isCurrentLineEmpty ? 2 : 4;
142
+ if (isPrevLineEmpty) {
143
+ linesToInsert = linesToInsert - 1;
144
+ }
145
+ if (isNextLineEmpty) {
146
+ linesToInsert = linesToInsert - 1;
147
+ }
148
+ editor.moveToLineEnd();
149
+ editor.insertAtCursor(editor.getNl(linesToInsert));
150
+ editor.restoreCursor(0, isCurrentLineEmpty ? line : line + 2);
151
+ }
152
+ function getListNumber(editor) {
153
+ const result = editor.getCurrentLine().match(/^(\d+\. )/);
154
+ return result ? result[1] : null;
155
+ }
156
+ function ulToggleFn(editor) {
157
+ if (editor.lineStartsWith('- ')) {
158
+ editor.removeFromLineBeginning(2);
159
+ } else {
160
+ const listNumber = getListNumber(editor);
161
+ if (listNumber) {
162
+ editor.removeFromLineBeginning(listNumber.length);
163
+ }
164
+ editor.insertAtLineBeginning('- ');
165
+ }
166
+ }
167
+ function olToggleFn(editor, n) {
168
+ const listNumber = getListNumber(editor);
169
+ if (listNumber) {
170
+ editor.removeFromLineBeginning(listNumber.length);
171
+ } else {
172
+ if (editor.lineStartsWith('- ')) {
173
+ editor.removeFromLineBeginning(2);
174
+ }
175
+ editor.insertAtLineBeginning((n || 1) + '. ');
176
+ }
177
+ }
178
+ function tableTemplate(nrows, ncols) {
179
+ const cellWidth = new Array(11);
180
+ const cell = ' ' + cellWidth.join(' ') + ' |';
181
+ const separatorCell = ' ' + cellWidth.join('-') + ' |';
182
+ let baseRow = '|';
183
+ let separatorRow = '|';
184
+ (0, _times.default)(ncols, ()=>{
185
+ baseRow += cell;
186
+ separatorRow += separatorCell;
187
+ });
188
+ const bodyRows = (0, _range.default)(nrows).map(()=>baseRow.replace(/\| {5}/g, '| Cell'));
189
+ const headerRow = baseRow.replace(/\| {7}/g, '| Header');
190
+ return [
191
+ headerRow,
192
+ separatorRow
193
+ ].concat(bodyRows);
194
+ }
195
+ function toggleHeader(editor, level) {
196
+ return ()=>{
197
+ const initialCh = editor.getCurrentCharacter();
198
+ const currentHeader = selectHeader(editor);
199
+ const prefix = (0, _repeat.default)(HEADER_CHAR, level);
200
+ if (!currentHeader) {
201
+ editor.moveToLineBeginning();
202
+ editor.insertAtCursor(prefix + ' ');
203
+ editor.restoreCursor(initialCh + prefix.length + 1);
204
+ return;
205
+ }
206
+ if (editor.getSelectedText() === prefix) {
207
+ editor.extendSelectionBy(1);
208
+ const removedCh = editor.getSelectionLength();
209
+ editor.removeSelectedText();
210
+ editor.restoreCursor(initialCh - removedCh);
211
+ return;
212
+ }
213
+ const diff = prefix.length - editor.getSelectionLength();
214
+ editor.replaceSelectedText(prefix);
215
+ editor.restoreCursor(initialCh + diff);
216
+ };
217
+ }
218
+ function selectHeader(editor) {
219
+ const result = editor.getCurrentLine().match(/^( {0,3})(#{1,6}) /);
220
+ if (!result) {
221
+ return null;
222
+ }
223
+ const indentation = result[1];
224
+ const header = result[2];
225
+ editor.select(getPos(0), getPos(header.length));
226
+ return editor.getSelection();
227
+ function getPos(modifier) {
228
+ return {
229
+ line: editor.getCurrentLineNumber(),
230
+ ch: indentation.length + modifier
231
+ };
232
+ }
233
+ }