markdown-actions 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.
@@ -0,0 +1,646 @@
1
+ var markdownActions = (() => {
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __export = (target, all) => {
21
+ for (var name in all)
22
+ __defProp(target, name, { get: all[name], enumerable: true });
23
+ };
24
+ var __copyProps = (to, from, except, desc) => {
25
+ if (from && typeof from === "object" || typeof from === "function") {
26
+ for (let key of __getOwnPropNames(from))
27
+ if (!__hasOwnProp.call(to, key) && key !== except)
28
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
29
+ }
30
+ return to;
31
+ };
32
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+
34
+ // src/index-new.js
35
+ var index_new_exports = {};
36
+ __export(index_new_exports, {
37
+ applyCustomFormat: () => applyCustomFormat,
38
+ default: () => index_new_default,
39
+ expandSelection: () => expandSelection,
40
+ getActiveFormats: () => getActiveFormats2,
41
+ hasFormat: () => hasFormat2,
42
+ insertHeader: () => insertHeader,
43
+ insertLink: () => insertLink,
44
+ preserveSelection: () => preserveSelection,
45
+ setUndoMethod: () => setUndoMethod,
46
+ toggleBold: () => toggleBold,
47
+ toggleBulletList: () => toggleBulletList,
48
+ toggleCode: () => toggleCode,
49
+ toggleItalic: () => toggleItalic,
50
+ toggleNumberedList: () => toggleNumberedList,
51
+ toggleQuote: () => toggleQuote
52
+ });
53
+
54
+ // src/core/style-args.js
55
+ function getDefaultStyleArgs() {
56
+ return {
57
+ prefix: "",
58
+ suffix: "",
59
+ blockPrefix: "",
60
+ blockSuffix: "",
61
+ multiline: false,
62
+ replaceNext: "",
63
+ prefixSpace: false,
64
+ scanFor: "",
65
+ surroundWithNewlines: false,
66
+ orderedList: false,
67
+ unorderedList: false,
68
+ trimFirst: false
69
+ };
70
+ }
71
+ function mergeStyleWithDefaults(style) {
72
+ return __spreadValues(__spreadValues({}, getDefaultStyleArgs()), style);
73
+ }
74
+
75
+ // src/core/selection-new.js
76
+ function isMultipleLines(string) {
77
+ return string.trim().split("\n").length > 1;
78
+ }
79
+ function repeat(string, n) {
80
+ return Array(n + 1).join(string);
81
+ }
82
+ function wordSelectionStart(text, i) {
83
+ let index = i;
84
+ while (text[index] && text[index - 1] != null && !text[index - 1].match(/\s/)) {
85
+ index--;
86
+ }
87
+ return index;
88
+ }
89
+ function wordSelectionEnd(text, i, multiline) {
90
+ let index = i;
91
+ const breakpoint = multiline ? /\n/ : /\s/;
92
+ while (text[index] && !text[index].match(breakpoint)) {
93
+ index++;
94
+ }
95
+ return index;
96
+ }
97
+ function expandSelectionToLine(textarea) {
98
+ const lines = textarea.value.split("\n");
99
+ let counter = 0;
100
+ for (let index = 0; index < lines.length; index++) {
101
+ const lineLength = lines[index].length + 1;
102
+ if (textarea.selectionStart >= counter && textarea.selectionStart < counter + lineLength) {
103
+ textarea.selectionStart = counter;
104
+ }
105
+ if (textarea.selectionEnd >= counter && textarea.selectionEnd < counter + lineLength) {
106
+ textarea.selectionEnd = counter + lineLength - 1;
107
+ }
108
+ counter += lineLength;
109
+ }
110
+ }
111
+ function expandSelectedText(textarea, prefixToUse, suffixToUse, multiline = false) {
112
+ if (textarea.selectionStart === textarea.selectionEnd) {
113
+ textarea.selectionStart = wordSelectionStart(textarea.value, textarea.selectionStart);
114
+ textarea.selectionEnd = wordSelectionEnd(textarea.value, textarea.selectionEnd, multiline);
115
+ } else {
116
+ const expandedSelectionStart = textarea.selectionStart - prefixToUse.length;
117
+ const expandedSelectionEnd = textarea.selectionEnd + suffixToUse.length;
118
+ const beginsWithPrefix = textarea.value.slice(expandedSelectionStart, textarea.selectionStart) === prefixToUse;
119
+ const endsWithSuffix = textarea.value.slice(textarea.selectionEnd, expandedSelectionEnd) === suffixToUse;
120
+ if (beginsWithPrefix && endsWithSuffix) {
121
+ textarea.selectionStart = expandedSelectionStart;
122
+ textarea.selectionEnd = expandedSelectionEnd;
123
+ }
124
+ }
125
+ return textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
126
+ }
127
+ function newlinesToSurroundSelectedText(textarea) {
128
+ const beforeSelection = textarea.value.slice(0, textarea.selectionStart);
129
+ const afterSelection = textarea.value.slice(textarea.selectionEnd);
130
+ const breaksBefore = beforeSelection.match(/\n*$/);
131
+ const breaksAfter = afterSelection.match(/^\n*/);
132
+ const newlinesBeforeSelection = breaksBefore ? breaksBefore[0].length : 0;
133
+ const newlinesAfterSelection = breaksAfter ? breaksAfter[0].length : 0;
134
+ let newlinesToAppend;
135
+ let newlinesToPrepend;
136
+ if (beforeSelection.match(/\S/) && newlinesBeforeSelection < 2) {
137
+ newlinesToAppend = repeat("\n", 2 - newlinesBeforeSelection);
138
+ }
139
+ if (afterSelection.match(/\S/) && newlinesAfterSelection < 2) {
140
+ newlinesToPrepend = repeat("\n", 2 - newlinesAfterSelection);
141
+ }
142
+ if (newlinesToAppend == null) {
143
+ newlinesToAppend = "";
144
+ }
145
+ if (newlinesToPrepend == null) {
146
+ newlinesToPrepend = "";
147
+ }
148
+ return { newlinesToAppend, newlinesToPrepend };
149
+ }
150
+ function preserveSelection(textarea, callback) {
151
+ const start = textarea.selectionStart;
152
+ const end = textarea.selectionEnd;
153
+ const scrollTop = textarea.scrollTop;
154
+ callback();
155
+ textarea.selectionStart = start;
156
+ textarea.selectionEnd = end;
157
+ textarea.scrollTop = scrollTop;
158
+ }
159
+
160
+ // src/core/insertion-new.js
161
+ var canInsertText = null;
162
+ function insertText(textarea, { text, selectionStart, selectionEnd }) {
163
+ const originalSelectionStart = textarea.selectionStart;
164
+ const before = textarea.value.slice(0, originalSelectionStart);
165
+ const after = textarea.value.slice(textarea.selectionEnd);
166
+ if (canInsertText === null || canInsertText === true) {
167
+ textarea.contentEditable = "true";
168
+ try {
169
+ canInsertText = document.execCommand("insertText", false, text);
170
+ } catch (error) {
171
+ canInsertText = false;
172
+ }
173
+ textarea.contentEditable = "false";
174
+ }
175
+ if (canInsertText && !textarea.value.slice(0, textarea.selectionStart).endsWith(text)) {
176
+ canInsertText = false;
177
+ }
178
+ if (!canInsertText) {
179
+ try {
180
+ document.execCommand("ms-beginUndoUnit");
181
+ } catch (e) {
182
+ }
183
+ textarea.value = before + text + after;
184
+ try {
185
+ document.execCommand("ms-endUndoUnit");
186
+ } catch (e) {
187
+ }
188
+ textarea.dispatchEvent(new CustomEvent("input", { bubbles: true, cancelable: true }));
189
+ }
190
+ if (selectionStart != null && selectionEnd != null) {
191
+ textarea.setSelectionRange(selectionStart, selectionEnd);
192
+ } else {
193
+ textarea.setSelectionRange(originalSelectionStart, textarea.selectionEnd);
194
+ }
195
+ }
196
+ function setUndoMethod(method) {
197
+ if (method === "native") {
198
+ canInsertText = true;
199
+ } else if (method === "manual") {
200
+ canInsertText = false;
201
+ } else {
202
+ canInsertText = null;
203
+ }
204
+ }
205
+
206
+ // src/operations/block-new.js
207
+ function blockStyle(textarea, arg) {
208
+ let newlinesToAppend;
209
+ let newlinesToPrepend;
210
+ const { prefix, suffix, blockPrefix, blockSuffix, replaceNext, prefixSpace, scanFor, surroundWithNewlines } = arg;
211
+ const originalSelectionStart = textarea.selectionStart;
212
+ const originalSelectionEnd = textarea.selectionEnd;
213
+ let selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
214
+ let prefixToUse = isMultipleLines(selectedText) && blockPrefix.length > 0 ? `${blockPrefix}
215
+ ` : prefix;
216
+ let suffixToUse = isMultipleLines(selectedText) && blockSuffix.length > 0 ? `
217
+ ${blockSuffix}` : suffix;
218
+ if (prefixSpace) {
219
+ const beforeSelection = textarea.value[textarea.selectionStart - 1];
220
+ if (textarea.selectionStart !== 0 && beforeSelection != null && !beforeSelection.match(/\s/)) {
221
+ prefixToUse = ` ${prefixToUse}`;
222
+ }
223
+ }
224
+ selectedText = expandSelectedText(textarea, prefixToUse, suffixToUse, arg.multiline);
225
+ let selectionStart = textarea.selectionStart;
226
+ let selectionEnd = textarea.selectionEnd;
227
+ const hasReplaceNext = replaceNext.length > 0 && suffixToUse.indexOf(replaceNext) > -1 && selectedText.length > 0;
228
+ if (surroundWithNewlines) {
229
+ const ref = newlinesToSurroundSelectedText(textarea);
230
+ newlinesToAppend = ref.newlinesToAppend;
231
+ newlinesToPrepend = ref.newlinesToPrepend;
232
+ prefixToUse = newlinesToAppend + prefix;
233
+ suffixToUse += newlinesToPrepend;
234
+ }
235
+ if (selectedText.startsWith(prefixToUse) && selectedText.endsWith(suffixToUse)) {
236
+ const replacementText = selectedText.slice(prefixToUse.length, selectedText.length - suffixToUse.length);
237
+ if (originalSelectionStart === originalSelectionEnd) {
238
+ let position = originalSelectionStart - prefixToUse.length;
239
+ position = Math.max(position, selectionStart);
240
+ position = Math.min(position, selectionStart + replacementText.length);
241
+ selectionStart = selectionEnd = position;
242
+ } else {
243
+ selectionEnd = selectionStart + replacementText.length;
244
+ }
245
+ return { text: replacementText, selectionStart, selectionEnd };
246
+ } else if (!hasReplaceNext) {
247
+ let replacementText = prefixToUse + selectedText + suffixToUse;
248
+ selectionStart = originalSelectionStart + prefixToUse.length;
249
+ selectionEnd = originalSelectionEnd + prefixToUse.length;
250
+ const whitespaceEdges = selectedText.match(/^\s*|\s*$/g);
251
+ if (arg.trimFirst && whitespaceEdges) {
252
+ const leadingWhitespace = whitespaceEdges[0] || "";
253
+ const trailingWhitespace = whitespaceEdges[1] || "";
254
+ replacementText = leadingWhitespace + prefixToUse + selectedText.trim() + suffixToUse + trailingWhitespace;
255
+ selectionStart += leadingWhitespace.length;
256
+ selectionEnd -= trailingWhitespace.length;
257
+ }
258
+ return { text: replacementText, selectionStart, selectionEnd };
259
+ } else if (scanFor.length > 0 && selectedText.match(scanFor)) {
260
+ suffixToUse = suffixToUse.replace(replaceNext, selectedText);
261
+ const replacementText = prefixToUse + selectedText + suffixToUse;
262
+ selectionStart = selectionEnd = selectionStart + prefixToUse.length;
263
+ return { text: replacementText, selectionStart, selectionEnd };
264
+ } else {
265
+ const replacementText = prefixToUse + selectedText + suffixToUse;
266
+ selectionStart = selectionStart + prefixToUse.length + selectedText.length + suffixToUse.indexOf(replaceNext);
267
+ selectionEnd = selectionStart + replaceNext.length;
268
+ return { text: replacementText, selectionStart, selectionEnd };
269
+ }
270
+ }
271
+ function multilineStyle(textarea, arg) {
272
+ const { prefix, suffix, surroundWithNewlines } = arg;
273
+ let text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
274
+ let selectionStart = textarea.selectionStart;
275
+ let selectionEnd = textarea.selectionEnd;
276
+ const lines = text.split("\n");
277
+ const undoStyle = lines.every((line) => line.startsWith(prefix) && line.endsWith(suffix));
278
+ if (undoStyle) {
279
+ text = lines.map((line) => line.slice(prefix.length, line.length - suffix.length)).join("\n");
280
+ selectionEnd = selectionStart + text.length;
281
+ } else {
282
+ text = lines.map((line) => prefix + line + suffix).join("\n");
283
+ if (surroundWithNewlines) {
284
+ const { newlinesToAppend, newlinesToPrepend } = newlinesToSurroundSelectedText(textarea);
285
+ selectionStart += newlinesToAppend.length;
286
+ selectionEnd = selectionStart + text.length;
287
+ text = newlinesToAppend + text + newlinesToPrepend;
288
+ }
289
+ }
290
+ return { text, selectionStart, selectionEnd };
291
+ }
292
+
293
+ // src/operations/list-new.js
294
+ function undoOrderedListStyle(text) {
295
+ const lines = text.split("\n");
296
+ const orderedListRegex = /^\d+\.\s+/;
297
+ const shouldUndoOrderedList = lines.every((line) => orderedListRegex.test(line));
298
+ let result = lines;
299
+ if (shouldUndoOrderedList) {
300
+ result = lines.map((line) => line.replace(orderedListRegex, ""));
301
+ }
302
+ return {
303
+ text: result.join("\n"),
304
+ processed: shouldUndoOrderedList
305
+ };
306
+ }
307
+ function undoUnorderedListStyle(text) {
308
+ const lines = text.split("\n");
309
+ const unorderedListPrefix = "- ";
310
+ const shouldUndoUnorderedList = lines.every((line) => line.startsWith(unorderedListPrefix));
311
+ let result = lines;
312
+ if (shouldUndoUnorderedList) {
313
+ result = lines.map((line) => line.slice(unorderedListPrefix.length, line.length));
314
+ }
315
+ return {
316
+ text: result.join("\n"),
317
+ processed: shouldUndoUnorderedList
318
+ };
319
+ }
320
+ function makePrefix(index, unorderedList) {
321
+ if (unorderedList) {
322
+ return "- ";
323
+ } else {
324
+ return `${index + 1}. `;
325
+ }
326
+ }
327
+ function clearExistingListStyle(style, selectedText) {
328
+ let undoResultOpositeList;
329
+ let undoResult;
330
+ let pristineText;
331
+ if (style.orderedList) {
332
+ undoResult = undoOrderedListStyle(selectedText);
333
+ undoResultOpositeList = undoUnorderedListStyle(undoResult.text);
334
+ pristineText = undoResultOpositeList.text;
335
+ } else {
336
+ undoResult = undoUnorderedListStyle(selectedText);
337
+ undoResultOpositeList = undoOrderedListStyle(undoResult.text);
338
+ pristineText = undoResultOpositeList.text;
339
+ }
340
+ return [undoResult, undoResultOpositeList, pristineText];
341
+ }
342
+ function listStyle(textarea, style) {
343
+ const noInitialSelection = textarea.selectionStart === textarea.selectionEnd;
344
+ let selectionStart = textarea.selectionStart;
345
+ let selectionEnd = textarea.selectionEnd;
346
+ expandSelectionToLine(textarea);
347
+ const selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
348
+ const [undoResult, undoResultOpositeList, pristineText] = clearExistingListStyle(style, selectedText);
349
+ const prefixedLines = pristineText.split("\n").map((value, index) => {
350
+ return `${makePrefix(index, style.unorderedList)}${value}`;
351
+ });
352
+ const totalPrefixLength = prefixedLines.reduce((previousValue, _currentValue, currentIndex) => {
353
+ return previousValue + makePrefix(currentIndex, style.unorderedList).length;
354
+ }, 0);
355
+ const totalPrefixLengthOpositeList = prefixedLines.reduce((previousValue, _currentValue, currentIndex) => {
356
+ return previousValue + makePrefix(currentIndex, !style.unorderedList).length;
357
+ }, 0);
358
+ if (undoResult.processed) {
359
+ if (noInitialSelection) {
360
+ selectionStart = Math.max(selectionStart - makePrefix(0, style.unorderedList).length, 0);
361
+ selectionEnd = selectionStart;
362
+ } else {
363
+ selectionStart = textarea.selectionStart;
364
+ selectionEnd = textarea.selectionEnd - totalPrefixLength;
365
+ }
366
+ return { text: pristineText, selectionStart, selectionEnd };
367
+ }
368
+ const { newlinesToAppend, newlinesToPrepend } = newlinesToSurroundSelectedText(textarea);
369
+ const text = newlinesToAppend + prefixedLines.join("\n") + newlinesToPrepend;
370
+ if (noInitialSelection) {
371
+ selectionStart = Math.max(selectionStart + makePrefix(0, style.unorderedList).length + newlinesToAppend.length, 0);
372
+ selectionEnd = selectionStart;
373
+ } else {
374
+ if (undoResultOpositeList.processed) {
375
+ selectionStart = Math.max(textarea.selectionStart + newlinesToAppend.length, 0);
376
+ selectionEnd = textarea.selectionEnd + newlinesToAppend.length + totalPrefixLength - totalPrefixLengthOpositeList;
377
+ } else {
378
+ selectionStart = Math.max(textarea.selectionStart + newlinesToAppend.length, 0);
379
+ selectionEnd = textarea.selectionEnd + newlinesToAppend.length + totalPrefixLength;
380
+ }
381
+ }
382
+ return { text, selectionStart, selectionEnd };
383
+ }
384
+
385
+ // src/core/style-text.js
386
+ function styleSelectedText(textarea, styleArgs) {
387
+ const text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd);
388
+ let result;
389
+ if (styleArgs.orderedList || styleArgs.unorderedList) {
390
+ result = listStyle(textarea, styleArgs);
391
+ } else if (styleArgs.multiline && isMultipleLines(text)) {
392
+ result = multilineStyle(textarea, styleArgs);
393
+ } else {
394
+ result = blockStyle(textarea, styleArgs);
395
+ }
396
+ insertText(textarea, result);
397
+ }
398
+
399
+ // src/core/detection.js
400
+ function getActiveFormats(textarea) {
401
+ if (!textarea)
402
+ return [];
403
+ const formats = [];
404
+ const { selectionStart, selectionEnd, value } = textarea;
405
+ const lines = value.split("\n");
406
+ let lineStart = 0;
407
+ let currentLine = "";
408
+ for (const line of lines) {
409
+ if (selectionStart >= lineStart && selectionStart <= lineStart + line.length) {
410
+ currentLine = line;
411
+ break;
412
+ }
413
+ lineStart += line.length + 1;
414
+ }
415
+ if (currentLine.startsWith("- ")) {
416
+ if (currentLine.startsWith("- [ ] ") || currentLine.startsWith("- [x] ")) {
417
+ formats.push("task-list");
418
+ } else {
419
+ formats.push("bullet-list");
420
+ }
421
+ }
422
+ if (/^\d+\.\s/.test(currentLine)) {
423
+ formats.push("numbered-list");
424
+ }
425
+ if (currentLine.startsWith("> ")) {
426
+ formats.push("quote");
427
+ }
428
+ if (currentLine.startsWith("# "))
429
+ formats.push("header");
430
+ if (currentLine.startsWith("## "))
431
+ formats.push("header-2");
432
+ if (currentLine.startsWith("### "))
433
+ formats.push("header-3");
434
+ const lookBehind = Math.max(0, selectionStart - 10);
435
+ const lookAhead = Math.min(value.length, selectionEnd + 10);
436
+ const surrounding = value.slice(lookBehind, lookAhead);
437
+ if (surrounding.includes("**")) {
438
+ const beforeCursor = value.slice(Math.max(0, selectionStart - 100), selectionStart);
439
+ const afterCursor = value.slice(selectionEnd, Math.min(value.length, selectionEnd + 100));
440
+ const lastOpenBold = beforeCursor.lastIndexOf("**");
441
+ const nextCloseBold = afterCursor.indexOf("**");
442
+ if (lastOpenBold !== -1 && nextCloseBold !== -1) {
443
+ formats.push("bold");
444
+ }
445
+ }
446
+ if (surrounding.includes("_")) {
447
+ const beforeCursor = value.slice(Math.max(0, selectionStart - 100), selectionStart);
448
+ const afterCursor = value.slice(selectionEnd, Math.min(value.length, selectionEnd + 100));
449
+ const lastOpenItalic = beforeCursor.lastIndexOf("_");
450
+ const nextCloseItalic = afterCursor.indexOf("_");
451
+ if (lastOpenItalic !== -1 && nextCloseItalic !== -1) {
452
+ formats.push("italic");
453
+ }
454
+ }
455
+ if (surrounding.includes("`")) {
456
+ const beforeCursor = value.slice(Math.max(0, selectionStart - 100), selectionStart);
457
+ const afterCursor = value.slice(selectionEnd, Math.min(value.length, selectionEnd + 100));
458
+ if (beforeCursor.includes("`") && afterCursor.includes("`")) {
459
+ formats.push("code");
460
+ }
461
+ }
462
+ if (surrounding.includes("[") && surrounding.includes("]")) {
463
+ const beforeCursor = value.slice(Math.max(0, selectionStart - 100), selectionStart);
464
+ const afterCursor = value.slice(selectionEnd, Math.min(value.length, selectionEnd + 100));
465
+ const lastOpenBracket = beforeCursor.lastIndexOf("[");
466
+ const nextCloseBracket = afterCursor.indexOf("]");
467
+ if (lastOpenBracket !== -1 && nextCloseBracket !== -1) {
468
+ const afterBracket = value.slice(selectionEnd + nextCloseBracket + 1, selectionEnd + nextCloseBracket + 10);
469
+ if (afterBracket.startsWith("(")) {
470
+ formats.push("link");
471
+ }
472
+ }
473
+ }
474
+ return formats;
475
+ }
476
+ function hasFormat(textarea, format) {
477
+ const activeFormats = getActiveFormats(textarea);
478
+ return activeFormats.includes(format);
479
+ }
480
+
481
+ // src/index-new.js
482
+ var FORMATS2 = {
483
+ bold: { prefix: "**", suffix: "**", trimFirst: true },
484
+ italic: { prefix: "_", suffix: "_", trimFirst: true },
485
+ code: {
486
+ prefix: "`",
487
+ suffix: "`",
488
+ blockPrefix: "```",
489
+ blockSuffix: "```"
490
+ },
491
+ link: {
492
+ prefix: "[",
493
+ suffix: "](url)",
494
+ replaceNext: "url",
495
+ scanFor: "https?://"
496
+ },
497
+ quote: {
498
+ prefix: "> ",
499
+ multiline: true,
500
+ surroundWithNewlines: true
501
+ },
502
+ bulletList: {
503
+ prefix: "- ",
504
+ multiline: true,
505
+ unorderedList: true
506
+ },
507
+ numberedList: {
508
+ prefix: "1. ",
509
+ multiline: true,
510
+ orderedList: true
511
+ },
512
+ header1: { prefix: "# " },
513
+ header2: { prefix: "## " },
514
+ header3: { prefix: "### " },
515
+ header4: { prefix: "#### " },
516
+ header5: { prefix: "##### " },
517
+ header6: { prefix: "###### " }
518
+ };
519
+ function applyStyle(textarea, format) {
520
+ const style = mergeStyleWithDefaults(format);
521
+ textarea.focus();
522
+ styleSelectedText(textarea, style);
523
+ }
524
+ function toggleBold(textarea) {
525
+ if (!textarea || textarea.disabled || textarea.readOnly)
526
+ return;
527
+ applyStyle(textarea, FORMATS2.bold);
528
+ }
529
+ function toggleItalic(textarea) {
530
+ if (!textarea || textarea.disabled || textarea.readOnly)
531
+ return;
532
+ applyStyle(textarea, FORMATS2.italic);
533
+ }
534
+ function toggleCode(textarea) {
535
+ if (!textarea || textarea.disabled || textarea.readOnly)
536
+ return;
537
+ applyStyle(textarea, FORMATS2.code);
538
+ }
539
+ function insertLink(textarea, options = {}) {
540
+ if (!textarea || textarea.disabled || textarea.readOnly)
541
+ return;
542
+ let format = __spreadValues({}, FORMATS2.link);
543
+ if (options.url) {
544
+ format.suffix = `](${options.url})`;
545
+ format.replaceNext = "";
546
+ }
547
+ if (options.text && !textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)) {
548
+ const pos = textarea.selectionStart;
549
+ textarea.value = textarea.value.slice(0, pos) + options.text + textarea.value.slice(pos);
550
+ textarea.selectionStart = pos;
551
+ textarea.selectionEnd = pos + options.text.length;
552
+ }
553
+ applyStyle(textarea, format);
554
+ }
555
+ function toggleBulletList(textarea) {
556
+ if (!textarea || textarea.disabled || textarea.readOnly)
557
+ return;
558
+ applyStyle(textarea, FORMATS2.bulletList);
559
+ }
560
+ function toggleNumberedList(textarea) {
561
+ if (!textarea || textarea.disabled || textarea.readOnly)
562
+ return;
563
+ applyStyle(textarea, FORMATS2.numberedList);
564
+ }
565
+ function toggleQuote(textarea) {
566
+ if (!textarea || textarea.disabled || textarea.readOnly)
567
+ return;
568
+ applyStyle(textarea, FORMATS2.quote);
569
+ }
570
+ function insertHeader(textarea, level = 1) {
571
+ if (!textarea || textarea.disabled || textarea.readOnly)
572
+ return;
573
+ if (level < 1 || level > 6)
574
+ level = 1;
575
+ const headerKey = `header${level}`;
576
+ const format = FORMATS2[headerKey] || FORMATS2.header1;
577
+ const value = textarea.value;
578
+ const start = textarea.selectionStart;
579
+ let lineStart = start;
580
+ let lineEnd = start;
581
+ while (lineStart > 0 && value[lineStart - 1] !== "\n") {
582
+ lineStart--;
583
+ }
584
+ while (lineEnd < value.length && value[lineEnd] !== "\n") {
585
+ lineEnd++;
586
+ }
587
+ textarea.selectionStart = lineStart;
588
+ textarea.selectionEnd = lineEnd;
589
+ const currentLine = value.slice(lineStart, lineEnd);
590
+ const cleanedLine = currentLine.replace(/^#{1,6}\s*/, "");
591
+ const newLine = format.prefix + cleanedLine;
592
+ const tempSelection = textarea.selectionStart;
593
+ textarea.value = value.slice(0, lineStart) + newLine + value.slice(lineEnd);
594
+ textarea.selectionStart = lineStart + format.prefix.length;
595
+ textarea.selectionEnd = lineStart + newLine.length;
596
+ }
597
+ function getActiveFormats2(textarea) {
598
+ return getActiveFormats(textarea);
599
+ }
600
+ function hasFormat2(textarea, format) {
601
+ return hasFormat(textarea, format);
602
+ }
603
+ function expandSelection(textarea, options = {}) {
604
+ if (!textarea)
605
+ return;
606
+ const { toWord, toLine } = options;
607
+ const { selectionStart, selectionEnd, value } = textarea;
608
+ if (toLine) {
609
+ expandSelectionToLine(textarea);
610
+ } else if (toWord && selectionStart === selectionEnd) {
611
+ let start = selectionStart;
612
+ let end = selectionEnd;
613
+ while (start > 0 && !/\s/.test(value[start - 1])) {
614
+ start--;
615
+ }
616
+ while (end < value.length && !/\s/.test(value[end])) {
617
+ end++;
618
+ }
619
+ textarea.selectionStart = start;
620
+ textarea.selectionEnd = end;
621
+ }
622
+ }
623
+ function applyCustomFormat(textarea, format) {
624
+ if (!textarea || textarea.disabled || textarea.readOnly)
625
+ return;
626
+ applyStyle(textarea, format);
627
+ }
628
+ var index_new_default = {
629
+ toggleBold,
630
+ toggleItalic,
631
+ toggleCode,
632
+ insertLink,
633
+ toggleBulletList,
634
+ toggleNumberedList,
635
+ toggleQuote,
636
+ insertHeader,
637
+ getActiveFormats: getActiveFormats2,
638
+ hasFormat: hasFormat2,
639
+ expandSelection,
640
+ applyCustomFormat,
641
+ preserveSelection,
642
+ setUndoMethod
643
+ };
644
+ return __toCommonJS(index_new_exports);
645
+ })();
646
+ //# sourceMappingURL=markdown-actions.js.map