UNPKG

@octopusdeploy/design-system-components

Version:
101 lines (100 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MarkdownProvider = void 0; class MarkdownProvider { value; selectionStart; selectionEnd; before; after; addNewLine; afterNewline; emptyText; constructor(value, selectionStart, selectionEnd) { this.value = value; this.selectionStart = selectionStart; this.selectionEnd = selectionEnd; this.before = ""; this.after = ""; this.addNewLine = false; this.afterNewline = ""; this.emptyText = ""; } insertBefore(before) { this.before = before; return this; } insertAfter(after) { this.after = after; return this; } surroundWithNewlines() { this.addNewLine = true; return this; } insertAfterNewlineInSelection(afterNewline) { this.afterNewline = afterNewline; return this; } emptySelectionText(emptyText) { this.emptyText = emptyText; return this; } apply(callback) { this.trimSelection(); if (this.emptyText && this.selectionStart === this.selectionEnd) { this.addEmptyText(); } if (this.addNewLine) { this.addNewLineImpl(); } this.value = this.splice(this.value, this.selectionEnd, this.after); this.value = this.splice(this.value, this.selectionStart, this.before); this.selectionStart = this.selectionStart + this.before.length; this.selectionEnd = this.selectionEnd + this.before.length; if (this.afterNewline) { this.insertAfterNewlineInSelectionImpl(); } callback(this.value, this.selectionStart, this.selectionEnd); } splice(input, start, newSubStr) { return input.slice(0, start) + newSubStr + input.slice(start); } addNewLineImpl() { // add a newline character for lists and comments, // but not if there is already one there const beforeSelection = this.value[this.selectionStart - 1]; if (beforeSelection !== "\n" && beforeSelection !== undefined) { this.value = this.splice(this.value, this.selectionStart, "\n"); this.selectionStart += 1; this.selectionEnd += 1; } const afterSelection = this.value[this.selectionEnd]; if (afterSelection !== "\n" && afterSelection !== undefined) { this.value = this.splice(this.value, this.selectionEnd, "\n"); } } trimSelection() { // trim whitespace from the selection (this was implemented in // other online markdown editors when I checked how they handled selection) while (this.value[this.selectionStart] === " " && this.selectionStart < this.selectionEnd) { this.selectionStart += 1; } while (this.value[this.selectionEnd - 1] === " " && this.selectionStart < this.selectionEnd) { this.selectionEnd -= 1; } } insertAfterNewlineInSelectionImpl() { // insert something after each newLine in the selection const selectedText = this.value.slice(this.selectionStart, this.selectionEnd); // eslint-disable-next-line no-control-regex const updatedText = selectedText.replace(new RegExp("\n", "g"), "\n" + this.afterNewline); this.value = this.value.slice(0, this.selectionStart) + updatedText + this.value.slice(this.selectionEnd); this.selectionEnd = this.selectionEnd - selectedText.length + updatedText.length; } addEmptyText() { this.value = this.splice(this.value, this.selectionStart, this.emptyText); this.selectionEnd = this.selectionEnd + this.emptyText.length; } } exports.MarkdownProvider = MarkdownProvider;