first commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
export function applyFormat(editor, format) {
|
||||
const wrap = (before, after = before, placeholder = "tekst") => {
|
||||
const start = editor.selectionStart, end = editor.selectionEnd;
|
||||
const selected = editor.value.slice(start, end) || placeholder;
|
||||
editor.setRangeText(before + selected + after, start, end, "select");
|
||||
};
|
||||
const prefix = (value) => {
|
||||
const start = editor.selectionStart, end = editor.selectionEnd;
|
||||
const lineStart = editor.value.lastIndexOf("\n", start - 1) + 1;
|
||||
const selected = editor.value.slice(lineStart, end);
|
||||
editor.setRangeText(selected.split("\n").map((line, index) => typeof value === "function" ? value(index) + line : value + line).join("\n"), lineStart, end, "select");
|
||||
};
|
||||
if (format === "bold") wrap("**");
|
||||
if (format === "italic") wrap("*");
|
||||
if (format === "strike") wrap("~~");
|
||||
if (format === "heading") prefix("## ");
|
||||
if (format === "bullet") prefix("- ");
|
||||
if (format === "number") prefix((index) => `${index + 1}. `);
|
||||
if (format === "quote") prefix("> ");
|
||||
if (format === "link") wrap("[", "](https://)", "opis linku");
|
||||
editor.focus();
|
||||
editor.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
}
|
||||
Reference in New Issue
Block a user