react-headless-mde 0.0.8 → 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.
- package/.github/workflows/npm-publish-next.yml +26 -0
- package/.github/workflows/npm-publish.yml +10 -8
- package/dist/cjs/index.js +92 -96
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +92 -96
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.ts +5 -8
- package/package.json +4 -6
- package/rollup.config.js +1 -1
- package/.prettierrc +0 -8
- /package/{readme.md → README.md} +0 -0
package/dist/esm/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { useRef, useMemo } from 'react';
|
|
|
7
7
|
function getSurroundingWord(text, position) {
|
|
8
8
|
if (!text)
|
|
9
9
|
throw Error("Argument 'text' should be truthy");
|
|
10
|
-
var isWordDelimiter = function (c) { return c ===
|
|
10
|
+
var isWordDelimiter = function (c) { return c === ' ' || c.charCodeAt(0) === 10; };
|
|
11
11
|
// leftIndex is initialized to 0 because if selection is 0, it won't even enter the iteration
|
|
12
12
|
var start = 0;
|
|
13
13
|
// rightIndex is initialized to text.length because if selection is equal to text.length it won't even enter the interation
|
|
@@ -47,7 +47,7 @@ function selectWord(_a) {
|
|
|
47
47
|
* to make sure there's an empty line between 'startPosition' and the previous text
|
|
48
48
|
*/
|
|
49
49
|
function getBreaksNeededForEmptyLineBefore(text, startPosition) {
|
|
50
|
-
if (text === void 0) { text =
|
|
50
|
+
if (text === void 0) { text = ''; }
|
|
51
51
|
if (startPosition === 0)
|
|
52
52
|
return 0;
|
|
53
53
|
// rules:
|
|
@@ -75,7 +75,7 @@ function getBreaksNeededForEmptyLineBefore(text, startPosition) {
|
|
|
75
75
|
* to make sure there's an empty line between 'startPosition' and the next text
|
|
76
76
|
*/
|
|
77
77
|
function getBreaksNeededForEmptyLineAfter(text, startPosition) {
|
|
78
|
-
if (text === void 0) { text =
|
|
78
|
+
if (text === void 0) { text = ''; }
|
|
79
79
|
if (startPosition === text.length - 1)
|
|
80
80
|
return 0;
|
|
81
81
|
// rules:
|
|
@@ -116,18 +116,18 @@ function insertBeforeEachLine(selectedText, insertBefore) {
|
|
|
116
116
|
var insertionLength = 0;
|
|
117
117
|
var modifiedText = lines
|
|
118
118
|
.map(function (item, index) {
|
|
119
|
-
if (typeof insertBefore ===
|
|
119
|
+
if (typeof insertBefore === 'string') {
|
|
120
120
|
insertionLength += insertBefore.length;
|
|
121
121
|
return insertBefore + item;
|
|
122
122
|
}
|
|
123
|
-
else if (typeof insertBefore ===
|
|
123
|
+
else if (typeof insertBefore === 'function') {
|
|
124
124
|
var insertionResult = insertBefore(item, index);
|
|
125
125
|
insertionLength += insertionResult.length;
|
|
126
126
|
return insertBefore(item, index) + item;
|
|
127
127
|
}
|
|
128
|
-
throw Error(
|
|
128
|
+
throw Error('insertion is expected to be either a string or a function');
|
|
129
129
|
})
|
|
130
|
-
.join(
|
|
130
|
+
.join('\n');
|
|
131
131
|
return { modifiedText: modifiedText, insertionLength: insertionLength };
|
|
132
132
|
}
|
|
133
133
|
|
|
@@ -147,7 +147,7 @@ function setHeader(initialState, api, prefix) {
|
|
|
147
147
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
148
148
|
var newSelectionRange = selectWord({
|
|
149
149
|
text: initialState.text,
|
|
150
|
-
selection: initialState.selection
|
|
150
|
+
selection: initialState.selection,
|
|
151
151
|
});
|
|
152
152
|
var state1 = api.setSelectionRange(newSelectionRange);
|
|
153
153
|
// Add the prefix to the selection
|
|
@@ -155,7 +155,7 @@ function setHeader(initialState, api, prefix) {
|
|
|
155
155
|
// Adjust the selection to not contain the prefix
|
|
156
156
|
api.setSelectionRange({
|
|
157
157
|
start: state2.selection.end - getSelectedText(state1).length,
|
|
158
|
-
end: state2.selection.end
|
|
158
|
+
end: state2.selection.end,
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
161
|
|
|
@@ -167,21 +167,21 @@ var headerHelpers = /*#__PURE__*/Object.freeze({
|
|
|
167
167
|
var headingLevel1Command = {
|
|
168
168
|
execute: function (_a) {
|
|
169
169
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
170
|
-
setHeader(initialState, textApi,
|
|
171
|
-
}
|
|
170
|
+
setHeader(initialState, textApi, '# ');
|
|
171
|
+
},
|
|
172
172
|
};
|
|
173
173
|
|
|
174
174
|
var boldCommand = {
|
|
175
175
|
shouldUndo: function (options) {
|
|
176
|
-
return (getCharactersBeforeSelection(options.initialState, 2) ===
|
|
177
|
-
getCharactersAfterSelection(options.initialState, 2) ===
|
|
176
|
+
return (getCharactersBeforeSelection(options.initialState, 2) === '**' &&
|
|
177
|
+
getCharactersAfterSelection(options.initialState, 2) === '**');
|
|
178
178
|
},
|
|
179
179
|
execute: function (_a) {
|
|
180
180
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
181
181
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
182
182
|
var newSelectionRange = selectWord({
|
|
183
183
|
text: initialState.text,
|
|
184
|
-
selection: initialState.selection
|
|
184
|
+
selection: initialState.selection,
|
|
185
185
|
});
|
|
186
186
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
187
187
|
// Replaces the current selection with the bold mark up
|
|
@@ -189,7 +189,7 @@ var boldCommand = {
|
|
|
189
189
|
// Adjust the selection to not contain the **
|
|
190
190
|
textApi.setSelectionRange({
|
|
191
191
|
start: state2.selection.end - 2 - getSelectedText(state1).length,
|
|
192
|
-
end: state2.selection.end - 2
|
|
192
|
+
end: state2.selection.end - 2,
|
|
193
193
|
});
|
|
194
194
|
},
|
|
195
195
|
undo: function (_a) {
|
|
@@ -197,27 +197,27 @@ var boldCommand = {
|
|
|
197
197
|
var text = getSelectedText(initialState);
|
|
198
198
|
textApi.setSelectionRange({
|
|
199
199
|
start: initialState.selection.start - 2,
|
|
200
|
-
end: initialState.selection.end + 2
|
|
200
|
+
end: initialState.selection.end + 2,
|
|
201
201
|
});
|
|
202
202
|
textApi.replaceSelection(text);
|
|
203
203
|
textApi.setSelectionRange({
|
|
204
204
|
start: initialState.selection.start - 2,
|
|
205
|
-
end: initialState.selection.end - 2
|
|
205
|
+
end: initialState.selection.end - 2,
|
|
206
206
|
});
|
|
207
|
-
}
|
|
207
|
+
},
|
|
208
208
|
};
|
|
209
209
|
|
|
210
210
|
var italicCommand = {
|
|
211
211
|
shouldUndo: function (options) {
|
|
212
|
-
return (getCharactersBeforeSelection(options.initialState, 1) ===
|
|
213
|
-
getCharactersAfterSelection(options.initialState, 1) ===
|
|
212
|
+
return (getCharactersBeforeSelection(options.initialState, 1) === '*' &&
|
|
213
|
+
getCharactersAfterSelection(options.initialState, 1) === '*');
|
|
214
214
|
},
|
|
215
215
|
execute: function (_a) {
|
|
216
216
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
217
217
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
218
218
|
var newSelectionRange = selectWord({
|
|
219
219
|
text: initialState.text,
|
|
220
|
-
selection: initialState.selection
|
|
220
|
+
selection: initialState.selection,
|
|
221
221
|
});
|
|
222
222
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
223
223
|
// Replaces the current selection with the italic mark up
|
|
@@ -225,7 +225,7 @@ var italicCommand = {
|
|
|
225
225
|
// Adjust the selection to not contain the *
|
|
226
226
|
textApi.setSelectionRange({
|
|
227
227
|
start: state2.selection.end - 1 - getSelectedText(state1).length,
|
|
228
|
-
end: state2.selection.end - 1
|
|
228
|
+
end: state2.selection.end - 1,
|
|
229
229
|
});
|
|
230
230
|
},
|
|
231
231
|
undo: function (_a) {
|
|
@@ -233,14 +233,14 @@ var italicCommand = {
|
|
|
233
233
|
var text = getSelectedText(initialState);
|
|
234
234
|
textApi.setSelectionRange({
|
|
235
235
|
start: initialState.selection.start - 1,
|
|
236
|
-
end: initialState.selection.end + 1
|
|
236
|
+
end: initialState.selection.end + 1,
|
|
237
237
|
});
|
|
238
238
|
textApi.replaceSelection(text);
|
|
239
239
|
textApi.setSelectionRange({
|
|
240
240
|
start: initialState.selection.start - 1,
|
|
241
|
-
end: initialState.selection.end - 1
|
|
241
|
+
end: initialState.selection.end - 1,
|
|
242
242
|
});
|
|
243
|
-
}
|
|
243
|
+
},
|
|
244
244
|
};
|
|
245
245
|
|
|
246
246
|
var strikethroughCommand = {
|
|
@@ -249,7 +249,7 @@ var strikethroughCommand = {
|
|
|
249
249
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
250
250
|
var newSelectionRange = selectWord({
|
|
251
251
|
text: initialState.text,
|
|
252
|
-
selection: initialState.selection
|
|
252
|
+
selection: initialState.selection,
|
|
253
253
|
});
|
|
254
254
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
255
255
|
// Replaces the current selection with the strikethrough mark up
|
|
@@ -257,9 +257,9 @@ var strikethroughCommand = {
|
|
|
257
257
|
// Adjust the selection to not contain the ~~
|
|
258
258
|
textApi.setSelectionRange({
|
|
259
259
|
start: state2.selection.end - 2 - getSelectedText(state1).length,
|
|
260
|
-
end: state2.selection.end - 2
|
|
260
|
+
end: state2.selection.end - 2,
|
|
261
261
|
});
|
|
262
|
-
}
|
|
262
|
+
},
|
|
263
263
|
};
|
|
264
264
|
|
|
265
265
|
var linkCommand = {
|
|
@@ -268,7 +268,7 @@ var linkCommand = {
|
|
|
268
268
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
269
269
|
var newSelectionRange = selectWord({
|
|
270
270
|
text: initialState.text,
|
|
271
|
-
selection: initialState.selection
|
|
271
|
+
selection: initialState.selection,
|
|
272
272
|
});
|
|
273
273
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
274
274
|
// Replaces the current selection with the bold mark up
|
|
@@ -276,9 +276,9 @@ var linkCommand = {
|
|
|
276
276
|
// Adjust the selection to not contain the **
|
|
277
277
|
textApi.setSelectionRange({
|
|
278
278
|
start: state2.selection.end - 6 - getSelectedText(state1).length,
|
|
279
|
-
end: state2.selection.end - 6
|
|
279
|
+
end: state2.selection.end - 6,
|
|
280
280
|
});
|
|
281
|
-
}
|
|
281
|
+
},
|
|
282
282
|
};
|
|
283
283
|
|
|
284
284
|
var quoteCommand = {
|
|
@@ -287,22 +287,22 @@ var quoteCommand = {
|
|
|
287
287
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
288
288
|
var newSelectionRange = selectWord({
|
|
289
289
|
text: initialState.text,
|
|
290
|
-
selection: initialState.selection
|
|
290
|
+
selection: initialState.selection,
|
|
291
291
|
});
|
|
292
292
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
293
293
|
var breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);
|
|
294
|
-
var breaksBefore = Array(breaksBeforeCount + 1).join(
|
|
294
|
+
var breaksBefore = Array(breaksBeforeCount + 1).join('\n');
|
|
295
295
|
var breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
|
|
296
|
-
var breaksAfter = Array(breaksAfterCount + 1).join(
|
|
296
|
+
var breaksAfter = Array(breaksAfterCount + 1).join('\n');
|
|
297
297
|
// Replaces the current selection with the quote mark up
|
|
298
298
|
textApi.replaceSelection("".concat(breaksBefore, "> ").concat(getSelectedText(state1)).concat(breaksAfter));
|
|
299
299
|
var selectionStart = state1.selection.start + breaksBeforeCount + 2;
|
|
300
300
|
var selectionEnd = selectionStart + getSelectedText(state1).length;
|
|
301
301
|
textApi.setSelectionRange({
|
|
302
302
|
start: selectionStart,
|
|
303
|
-
end: selectionEnd
|
|
303
|
+
end: selectionEnd,
|
|
304
304
|
});
|
|
305
|
-
}
|
|
305
|
+
},
|
|
306
306
|
};
|
|
307
307
|
|
|
308
308
|
var imageCommand = {
|
|
@@ -311,17 +311,17 @@ var imageCommand = {
|
|
|
311
311
|
// Replaces the current selection with the whole word selected
|
|
312
312
|
var state1 = textApi.setSelectionRange(selectWord({
|
|
313
313
|
text: initialState.text,
|
|
314
|
-
selection: initialState.selection
|
|
314
|
+
selection: initialState.selection,
|
|
315
315
|
}));
|
|
316
316
|
// Replaces the current selection with the image
|
|
317
|
-
var imageTemplate = getSelectedText(state1) ||
|
|
317
|
+
var imageTemplate = getSelectedText(state1) || 'https://example.com/your-image.png';
|
|
318
318
|
textApi.replaceSelection(""));
|
|
319
319
|
// Adjust the selection to not contain the **
|
|
320
320
|
textApi.setSelectionRange({
|
|
321
321
|
start: state1.selection.start + 4,
|
|
322
|
-
end: state1.selection.start + 4 + imageTemplate.length
|
|
322
|
+
end: state1.selection.start + 4 + imageTemplate.length,
|
|
323
323
|
});
|
|
324
|
-
}
|
|
324
|
+
},
|
|
325
325
|
};
|
|
326
326
|
|
|
327
327
|
/******************************************************************************
|
|
@@ -355,7 +355,7 @@ function __generator(thisArg, body) {
|
|
|
355
355
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
356
356
|
function step(op) {
|
|
357
357
|
if (f) throw new TypeError("Generator is already executing.");
|
|
358
|
-
while (_) try {
|
|
358
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
359
359
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
360
360
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
361
361
|
switch (op[0]) {
|
|
@@ -379,9 +379,7 @@ function __generator(thisArg, body) {
|
|
|
379
379
|
|
|
380
380
|
var CommandController = /** @class */ (function () {
|
|
381
381
|
function CommandController(textController, commandMap) {
|
|
382
|
-
|
|
383
|
-
* Indicates whether there is a command currently executing
|
|
384
|
-
*/
|
|
382
|
+
// Indicates whether there is a command currently executing
|
|
385
383
|
this.isExecuting = false;
|
|
386
384
|
this.textController = textController;
|
|
387
385
|
this.commandMap = commandMap;
|
|
@@ -395,8 +393,8 @@ var CommandController = /** @class */ (function () {
|
|
|
395
393
|
case 0:
|
|
396
394
|
if (this.isExecuting) {
|
|
397
395
|
// The simplest thing to do is to ignore commands while
|
|
398
|
-
// there is already a command
|
|
399
|
-
//
|
|
396
|
+
// there is already a command executing.
|
|
397
|
+
// The alternative would be to queue commands
|
|
400
398
|
// but there is no guarantee that the state after one command executes will still be compatible
|
|
401
399
|
// with the next one. In fact, it is likely not to be.
|
|
402
400
|
return [2 /*return*/];
|
|
@@ -407,7 +405,7 @@ var CommandController = /** @class */ (function () {
|
|
|
407
405
|
}
|
|
408
406
|
executeOptions = {
|
|
409
407
|
initialState: this.textController.getState(),
|
|
410
|
-
textApi: this.textController
|
|
408
|
+
textApi: this.textController,
|
|
411
409
|
};
|
|
412
410
|
if (!(((_a = command.shouldUndo) === null || _a === void 0 ? void 0 : _a.call(command, executeOptions)) && (command === null || command === void 0 ? void 0 : command.undo))) return [3 /*break*/, 1];
|
|
413
411
|
command.undo(executeOptions);
|
|
@@ -431,7 +429,7 @@ var TextAreaTextController = /** @class */ (function () {
|
|
|
431
429
|
TextAreaTextController.prototype.replaceSelection = function (text) {
|
|
432
430
|
var textArea = this.textAreaRef.current;
|
|
433
431
|
if (!textArea) {
|
|
434
|
-
throw new Error(
|
|
432
|
+
throw new Error('TextAreaRef is not set');
|
|
435
433
|
}
|
|
436
434
|
insertText(textArea, text);
|
|
437
435
|
return getStateFromTextArea(textArea);
|
|
@@ -439,7 +437,7 @@ var TextAreaTextController = /** @class */ (function () {
|
|
|
439
437
|
TextAreaTextController.prototype.setSelectionRange = function (selection) {
|
|
440
438
|
var textArea = this.textAreaRef.current;
|
|
441
439
|
if (!textArea) {
|
|
442
|
-
throw new Error(
|
|
440
|
+
throw new Error('TextAreaRef is not set');
|
|
443
441
|
}
|
|
444
442
|
textArea.focus();
|
|
445
443
|
textArea.selectionStart = selection.start;
|
|
@@ -449,7 +447,7 @@ var TextAreaTextController = /** @class */ (function () {
|
|
|
449
447
|
TextAreaTextController.prototype.getState = function () {
|
|
450
448
|
var textArea = this.textAreaRef.current;
|
|
451
449
|
if (!textArea) {
|
|
452
|
-
throw new Error(
|
|
450
|
+
throw new Error('TextAreaRef is not set');
|
|
453
451
|
}
|
|
454
452
|
return getStateFromTextArea(textArea);
|
|
455
453
|
};
|
|
@@ -459,9 +457,9 @@ function getStateFromTextArea(textArea) {
|
|
|
459
457
|
return {
|
|
460
458
|
selection: {
|
|
461
459
|
start: textArea.selectionStart,
|
|
462
|
-
end: textArea.selectionEnd
|
|
460
|
+
end: textArea.selectionEnd,
|
|
463
461
|
},
|
|
464
|
-
text: textArea.value
|
|
462
|
+
text: textArea.value,
|
|
465
463
|
};
|
|
466
464
|
}
|
|
467
465
|
/**
|
|
@@ -485,12 +483,12 @@ function insertText(input, text) {
|
|
|
485
483
|
return;
|
|
486
484
|
}
|
|
487
485
|
// Webkit + Edge
|
|
488
|
-
var isSuccess = document.execCommand(
|
|
486
|
+
var isSuccess = document.execCommand('insertText', false, text);
|
|
489
487
|
if (!isSuccess) {
|
|
490
488
|
var start = input.selectionStart || 0;
|
|
491
489
|
var end = input.selectionEnd || 0;
|
|
492
490
|
// Firefox (non-standard method)
|
|
493
|
-
if (typeof input.setRangeText ===
|
|
491
|
+
if (typeof input.setRangeText === 'function') {
|
|
494
492
|
input.setRangeText(text);
|
|
495
493
|
}
|
|
496
494
|
else {
|
|
@@ -539,8 +537,8 @@ function insertText(input, text) {
|
|
|
539
537
|
// Correct the cursor position to be at the end of the insertion
|
|
540
538
|
input.setSelectionRange(start + text.length, start + text.length);
|
|
541
539
|
// Notify any possible listeners of the change
|
|
542
|
-
var e = document.createEvent(
|
|
543
|
-
e.initEvent(
|
|
540
|
+
var e = document.createEvent('UIEvent');
|
|
541
|
+
e.initEvent('input', true, false);
|
|
544
542
|
input.dispatchEvent(e);
|
|
545
543
|
}
|
|
546
544
|
}
|
|
@@ -550,13 +548,13 @@ function insertText(input, text) {
|
|
|
550
548
|
* Copied from https://github.com/grassator/insert-text-at-cursor
|
|
551
549
|
*/
|
|
552
550
|
function canManipulateViaTextNodes(input) {
|
|
553
|
-
if (input.nodeName !==
|
|
551
|
+
if (input.nodeName !== 'TEXTAREA') {
|
|
554
552
|
return false;
|
|
555
553
|
}
|
|
556
554
|
var browserSupportsTextareaTextNodes;
|
|
557
|
-
if (typeof browserSupportsTextareaTextNodes ===
|
|
558
|
-
var textarea = document.createElement(
|
|
559
|
-
textarea.value =
|
|
555
|
+
if (typeof browserSupportsTextareaTextNodes === 'undefined') {
|
|
556
|
+
var textarea = document.createElement('textarea');
|
|
557
|
+
textarea.value = '1';
|
|
560
558
|
browserSupportsTextareaTextNodes = !!textarea.firstChild;
|
|
561
559
|
}
|
|
562
560
|
return browserSupportsTextareaTextNodes;
|
|
@@ -566,25 +564,23 @@ function makeList(state0, textController, insertBefore) {
|
|
|
566
564
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
567
565
|
var newSelectionRange = selectWord({
|
|
568
566
|
text: state0.text,
|
|
569
|
-
selection: state0.selection
|
|
567
|
+
selection: state0.selection,
|
|
570
568
|
});
|
|
571
569
|
var state1 = textController.setSelectionRange(newSelectionRange);
|
|
572
570
|
var breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);
|
|
573
|
-
var breaksBefore = Array(breaksBeforeCount + 1).join(
|
|
571
|
+
var breaksBefore = Array(breaksBeforeCount + 1).join('\n');
|
|
574
572
|
var breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
|
|
575
|
-
var breaksAfter = Array(breaksAfterCount + 1).join(
|
|
573
|
+
var breaksAfter = Array(breaksAfterCount + 1).join('\n');
|
|
576
574
|
var modifiedText = insertBeforeEachLine(getSelectedText(state1), insertBefore);
|
|
577
575
|
textController.replaceSelection("".concat(breaksBefore).concat(modifiedText.modifiedText).concat(breaksAfter));
|
|
578
576
|
// Specifically when the text has only one line, we can exclude the "- ", for example, from the selection
|
|
579
|
-
var oneLinerOffset = getSelectedText(state1).indexOf(
|
|
580
|
-
? modifiedText.insertionLength
|
|
581
|
-
: 0;
|
|
577
|
+
var oneLinerOffset = getSelectedText(state1).indexOf('\n') === -1 ? modifiedText.insertionLength : 0;
|
|
582
578
|
var selectionStart = state1.selection.start + breaksBeforeCount + oneLinerOffset;
|
|
583
579
|
var selectionEnd = selectionStart + modifiedText.modifiedText.length - oneLinerOffset;
|
|
584
580
|
// Adjust the selection to not contain the **
|
|
585
581
|
textController.setSelectionRange({
|
|
586
582
|
start: selectionStart,
|
|
587
|
-
end: selectionEnd
|
|
583
|
+
end: selectionEnd,
|
|
588
584
|
});
|
|
589
585
|
}
|
|
590
586
|
|
|
@@ -595,15 +591,15 @@ var listHelpers = /*#__PURE__*/Object.freeze({
|
|
|
595
591
|
|
|
596
592
|
var codeCommand = {
|
|
597
593
|
shouldUndo: function (options) {
|
|
598
|
-
return (getCharactersBeforeSelection(options.initialState, 1) ===
|
|
599
|
-
getCharactersAfterSelection(options.initialState, 1) ===
|
|
594
|
+
return (getCharactersBeforeSelection(options.initialState, 1) === '`' &&
|
|
595
|
+
getCharactersAfterSelection(options.initialState, 1) === '`');
|
|
600
596
|
},
|
|
601
597
|
execute: function (_a) {
|
|
602
598
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
603
599
|
// Adjust the selection to encompass the whole word if the caret is inside one
|
|
604
600
|
var newSelectionRange = selectWord({
|
|
605
601
|
text: initialState.text,
|
|
606
|
-
selection: initialState.selection
|
|
602
|
+
selection: initialState.selection,
|
|
607
603
|
});
|
|
608
604
|
var state1 = textApi.setSelectionRange(newSelectionRange);
|
|
609
605
|
// Replaces the current selection with the italic mark up
|
|
@@ -611,7 +607,7 @@ var codeCommand = {
|
|
|
611
607
|
// Adjust the selection to not contain the *
|
|
612
608
|
textApi.setSelectionRange({
|
|
613
609
|
start: state2.selection.end - 1 - getSelectedText(state1).length,
|
|
614
|
-
end: state2.selection.end - 1
|
|
610
|
+
end: state2.selection.end - 1,
|
|
615
611
|
});
|
|
616
612
|
},
|
|
617
613
|
undo: function (_a) {
|
|
@@ -619,14 +615,14 @@ var codeCommand = {
|
|
|
619
615
|
var text = getSelectedText(initialState);
|
|
620
616
|
textApi.setSelectionRange({
|
|
621
617
|
start: initialState.selection.start - 1,
|
|
622
|
-
end: initialState.selection.end + 1
|
|
618
|
+
end: initialState.selection.end + 1,
|
|
623
619
|
});
|
|
624
620
|
textApi.replaceSelection(text);
|
|
625
621
|
textApi.setSelectionRange({
|
|
626
622
|
start: initialState.selection.start - 1,
|
|
627
|
-
end: initialState.selection.end - 1
|
|
623
|
+
end: initialState.selection.end - 1,
|
|
628
624
|
});
|
|
629
|
-
}
|
|
625
|
+
},
|
|
630
626
|
};
|
|
631
627
|
|
|
632
628
|
function useTextAreaMarkdownEditor(options) {
|
|
@@ -638,7 +634,7 @@ function useTextAreaMarkdownEditor(options) {
|
|
|
638
634
|
return {
|
|
639
635
|
textController: textController,
|
|
640
636
|
commandController: commandController,
|
|
641
|
-
ref: textAreaRef
|
|
637
|
+
ref: textAreaRef,
|
|
642
638
|
};
|
|
643
639
|
}
|
|
644
640
|
|
|
@@ -650,91 +646,91 @@ var codeBlockCommand = {
|
|
|
650
646
|
return __generator(this, function (_b) {
|
|
651
647
|
newSelectionRange = selectWord({
|
|
652
648
|
text: initialState.text,
|
|
653
|
-
selection: initialState.selection
|
|
649
|
+
selection: initialState.selection,
|
|
654
650
|
});
|
|
655
651
|
state1 = textApi.setSelectionRange(newSelectionRange);
|
|
656
652
|
// when there's no breaking line
|
|
657
|
-
if (getSelectedText(state1).indexOf(
|
|
653
|
+
if (getSelectedText(state1).indexOf('\n') === -1) {
|
|
658
654
|
textApi.replaceSelection("`".concat(getSelectedText(state1), "`"));
|
|
659
655
|
selectionStart_1 = state1.selection.start + 1;
|
|
660
656
|
selectionEnd_1 = selectionStart_1 + getSelectedText(state1).length;
|
|
661
657
|
textApi.setSelectionRange({
|
|
662
658
|
start: selectionStart_1,
|
|
663
|
-
end: selectionEnd_1
|
|
659
|
+
end: selectionEnd_1,
|
|
664
660
|
});
|
|
665
661
|
return [2 /*return*/];
|
|
666
662
|
}
|
|
667
663
|
breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);
|
|
668
|
-
breaksBefore = Array(breaksBeforeCount + 1).join(
|
|
664
|
+
breaksBefore = Array(breaksBeforeCount + 1).join('\n');
|
|
669
665
|
breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);
|
|
670
|
-
breaksAfter = Array(breaksAfterCount + 1).join(
|
|
666
|
+
breaksAfter = Array(breaksAfterCount + 1).join('\n');
|
|
671
667
|
textApi.replaceSelection("".concat(breaksBefore, "```\n").concat(getSelectedText(state1), "\n```").concat(breaksAfter));
|
|
672
668
|
selectionStart = state1.selection.start + breaksBeforeCount + 4;
|
|
673
669
|
selectionEnd = selectionStart + getSelectedText(state1).length;
|
|
674
670
|
textApi.setSelectionRange({
|
|
675
671
|
start: selectionStart,
|
|
676
|
-
end: selectionEnd
|
|
672
|
+
end: selectionEnd,
|
|
677
673
|
});
|
|
678
674
|
return [2 /*return*/];
|
|
679
675
|
});
|
|
680
676
|
});
|
|
681
|
-
}
|
|
677
|
+
},
|
|
682
678
|
};
|
|
683
679
|
|
|
684
680
|
var checkedListCommand = {
|
|
685
681
|
execute: function (_a) {
|
|
686
682
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
687
683
|
makeList(initialState, textApi, function () { return "- [ ] "; });
|
|
688
|
-
}
|
|
684
|
+
},
|
|
689
685
|
};
|
|
690
686
|
|
|
691
687
|
var orderedListCommand = {
|
|
692
688
|
execute: function (_a) {
|
|
693
689
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
694
690
|
makeList(initialState, textApi, function (item, index) { return "".concat(index + 1, ". "); });
|
|
695
|
-
}
|
|
691
|
+
},
|
|
696
692
|
};
|
|
697
693
|
|
|
698
694
|
var unorderedListCommand = {
|
|
699
695
|
execute: function (_a) {
|
|
700
696
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
701
|
-
makeList(initialState, textApi,
|
|
702
|
-
}
|
|
697
|
+
makeList(initialState, textApi, '- ');
|
|
698
|
+
},
|
|
703
699
|
};
|
|
704
700
|
|
|
705
701
|
var headingLevel2Command = {
|
|
706
702
|
execute: function (_a) {
|
|
707
703
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
708
|
-
setHeader(initialState, textApi,
|
|
709
|
-
}
|
|
704
|
+
setHeader(initialState, textApi, '## ');
|
|
705
|
+
},
|
|
710
706
|
};
|
|
711
707
|
|
|
712
708
|
var headingLevel3Command = {
|
|
713
709
|
execute: function (_a) {
|
|
714
710
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
715
|
-
setHeader(initialState, textApi,
|
|
716
|
-
}
|
|
711
|
+
setHeader(initialState, textApi, '### ');
|
|
712
|
+
},
|
|
717
713
|
};
|
|
718
714
|
|
|
719
715
|
var headingLevel4Command = {
|
|
720
716
|
execute: function (_a) {
|
|
721
717
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
722
|
-
setHeader(initialState, textApi,
|
|
723
|
-
}
|
|
718
|
+
setHeader(initialState, textApi, '#### ');
|
|
719
|
+
},
|
|
724
720
|
};
|
|
725
721
|
|
|
726
722
|
var headingLevel5Command = {
|
|
727
723
|
execute: function (_a) {
|
|
728
724
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
729
|
-
setHeader(initialState, textApi,
|
|
730
|
-
}
|
|
725
|
+
setHeader(initialState, textApi, '##### ');
|
|
726
|
+
},
|
|
731
727
|
};
|
|
732
728
|
|
|
733
729
|
var headingLevel6Command = {
|
|
734
730
|
execute: function (_a) {
|
|
735
731
|
var initialState = _a.initialState, textApi = _a.textApi;
|
|
736
|
-
setHeader(initialState, textApi,
|
|
737
|
-
}
|
|
732
|
+
setHeader(initialState, textApi, '###### ');
|
|
733
|
+
},
|
|
738
734
|
};
|
|
739
735
|
|
|
740
736
|
export { CommandController, TextAreaTextController, boldCommand, checkedListCommand, codeBlockCommand, codeCommand, headerHelpers, headingLevel1Command, headingLevel2Command, headingLevel3Command, headingLevel4Command, headingLevel5Command, headingLevel6Command, imageCommand, italicCommand, linkCommand, listHelpers, orderedListCommand, quoteCommand, strikethroughCommand, textHelpers, unorderedListCommand, useTextAreaMarkdownEditor };
|