2023-11-21 11:24:08 +00:00
|
|
|
import {type Value} from "@holllo/webextension-storage";
|
2023-06-23 10:52:03 +00:00
|
|
|
import {render} from "preact";
|
|
|
|
import {log, querySelectorAll} from "../../utilities/exports.js";
|
2023-11-21 11:24:08 +00:00
|
|
|
import {
|
|
|
|
type MarkdownSnippet,
|
|
|
|
MarkdownSnippetMarker,
|
|
|
|
} from "../../storage/exports.js";
|
|
|
|
|
|
|
|
export function runMarkdownToolbarFeature(
|
|
|
|
snippets: Array<Value<MarkdownSnippet>>,
|
|
|
|
) {
|
|
|
|
const count = addToolbarsToTextareas(snippets);
|
2023-11-23 12:06:10 +00:00
|
|
|
if (count > 0) {
|
|
|
|
log(`Markdown Toolbar: Initialized for ${count} textareas.`);
|
|
|
|
}
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 11:24:08 +00:00
|
|
|
function addToolbarsToTextareas(
|
|
|
|
snippets: Array<Value<MarkdownSnippet>>,
|
|
|
|
): number {
|
2020-10-10 23:32:27 +00:00
|
|
|
// Grab all Markdown forms that don't have already have a toolbar.
|
2023-06-23 10:52:03 +00:00
|
|
|
const markdownForms = querySelectorAll(".form-markdown:not(.trx-toolbar)");
|
2020-10-10 23:32:27 +00:00
|
|
|
if (markdownForms.length === 0) {
|
2022-02-23 22:41:32 +00:00
|
|
|
return 0;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const form of markdownForms) {
|
|
|
|
// Add `trx-toolbar` to indicate this Markdown form already has the toolbar.
|
2023-06-23 10:52:03 +00:00
|
|
|
form.classList.add("trx-toolbar");
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const menu = form.querySelector<HTMLElement>(".tab-markdown-mode")!;
|
|
|
|
const textarea = form.querySelector<HTMLTextAreaElement>(
|
2022-02-23 13:52:06 +00:00
|
|
|
'textarea[name="markdown"]',
|
2020-10-10 23:32:27 +00:00
|
|
|
)!;
|
|
|
|
|
|
|
|
const snippetButtons = snippets
|
2023-11-21 11:24:08 +00:00
|
|
|
.filter((snippet) => !snippet.value.inDropdown)
|
2023-06-23 10:52:03 +00:00
|
|
|
.map((snippet) => (
|
2023-11-21 11:24:08 +00:00
|
|
|
<SnippetButton
|
|
|
|
allSnippets={snippets}
|
|
|
|
snippet={snippet}
|
|
|
|
textarea={textarea}
|
|
|
|
/>
|
2023-06-23 10:52:03 +00:00
|
|
|
));
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2023-11-21 11:24:08 +00:00
|
|
|
const noDropdownSnippets = snippets.length === snippetButtons.length;
|
|
|
|
|
2020-10-10 23:32:27 +00:00
|
|
|
// Render the buttons inside the tab menu so they appear
|
|
|
|
// next to the Edit and Preview buttons.
|
2023-06-23 10:52:03 +00:00
|
|
|
const menuPlaceholder = document.createElement("div");
|
2020-10-10 23:32:27 +00:00
|
|
|
menu.append(menuPlaceholder);
|
|
|
|
render(snippetButtons, menu, menuPlaceholder);
|
|
|
|
|
2023-11-21 11:24:08 +00:00
|
|
|
if (!noDropdownSnippets) {
|
|
|
|
// And render the dropdown directly after the menu.
|
|
|
|
const dropdownPlaceholder = document.createElement("div");
|
|
|
|
const menuParent = menu.parentElement!;
|
|
|
|
menu.after(dropdownPlaceholder);
|
|
|
|
render(
|
|
|
|
<>
|
|
|
|
<SnippetDropdown allSnippets={snippets} textarea={textarea} />
|
|
|
|
</>,
|
|
|
|
menuParent,
|
|
|
|
dropdownPlaceholder,
|
|
|
|
);
|
|
|
|
}
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
2022-02-23 22:41:32 +00:00
|
|
|
|
|
|
|
return markdownForms.length;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Props = {
|
2023-11-21 11:24:08 +00:00
|
|
|
allSnippets: Array<Value<MarkdownSnippet>>;
|
|
|
|
snippet?: Value<MarkdownSnippet>;
|
2020-10-10 23:32:27 +00:00
|
|
|
textarea: HTMLTextAreaElement;
|
|
|
|
};
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
function SnippetButton(props: Required<Props>) {
|
2020-10-10 23:32:27 +00:00
|
|
|
const click = (event: MouseEvent) => {
|
|
|
|
event.preventDefault();
|
|
|
|
insertSnippet(props);
|
|
|
|
};
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
return (
|
2022-02-23 13:52:06 +00:00
|
|
|
<li class="tab-item">
|
2023-06-23 10:52:03 +00:00
|
|
|
<button class="btn btn-link" onClick={click}>
|
2023-11-21 11:24:08 +00:00
|
|
|
{props.snippet.value.name}
|
2022-02-23 13:52:06 +00:00
|
|
|
</button>
|
|
|
|
</li>
|
2023-06-23 10:52:03 +00:00
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
function SnippetDropdown(props: Props) {
|
2023-11-21 11:24:08 +00:00
|
|
|
const snippets = props.allSnippets;
|
|
|
|
const options = snippets
|
|
|
|
?.filter((snippet) => snippet.value.inDropdown)
|
|
|
|
.map((snippet) => (
|
|
|
|
<option value={snippet.value.name}>{snippet.value.name}</option>
|
|
|
|
));
|
|
|
|
|
|
|
|
if (options.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
const change = (event: Event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const snippet = snippets.find(
|
2023-11-21 11:24:08 +00:00
|
|
|
(value) => value.value.name === (event.target as HTMLSelectElement).value,
|
2020-10-10 23:32:27 +00:00
|
|
|
)!;
|
|
|
|
|
|
|
|
insertSnippet({
|
|
|
|
...props,
|
2022-02-23 13:52:06 +00:00
|
|
|
snippet,
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
(event.target as HTMLSelectElement).selectedIndex = 0;
|
|
|
|
};
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
return (
|
|
|
|
<select class="form-select" onChange={change}>
|
2022-02-23 13:52:06 +00:00
|
|
|
<option>More…</option>
|
2023-06-23 10:52:03 +00:00
|
|
|
{options}
|
2022-02-23 13:52:06 +00:00
|
|
|
</select>
|
2023-06-23 10:52:03 +00:00
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function insertSnippet(props: Required<Props>) {
|
|
|
|
const {textarea, snippet} = props;
|
|
|
|
const {selectionStart, selectionEnd} = textarea;
|
|
|
|
|
|
|
|
// Since you have to press a button or go into a dropdown to click on a
|
|
|
|
// snippet, the textarea won't be focused anymore. So focus it again.
|
|
|
|
textarea.focus();
|
|
|
|
|
2023-11-21 11:24:08 +00:00
|
|
|
let {markdown} = snippet.value;
|
|
|
|
|
|
|
|
// Get the marker positions and remove them from the snippet.
|
|
|
|
let cursorIndex = markdown.indexOf(MarkdownSnippetMarker.Cursor);
|
|
|
|
markdown = markdown.replace(MarkdownSnippetMarker.Cursor, "");
|
|
|
|
const selectedCursorIndex = markdown.indexOf(
|
|
|
|
MarkdownSnippetMarker.SelectedCursor,
|
|
|
|
);
|
|
|
|
markdown = markdown.replace(MarkdownSnippetMarker.SelectedCursor, "");
|
|
|
|
|
|
|
|
// If we have a Cursor and SelectedCursor in the snippet, and the Cursor is
|
|
|
|
// placed after the SelectedCursor we have to account for the marker string
|
|
|
|
// length.
|
|
|
|
// We don't have to do it in reverse because the Cursor index is taken first
|
|
|
|
// and the marker string for that is removed before the SelectedCursor index
|
|
|
|
// is taken.
|
|
|
|
if (
|
|
|
|
cursorIndex !== -1 &&
|
|
|
|
selectedCursorIndex !== -1 &&
|
|
|
|
cursorIndex > selectedCursorIndex
|
|
|
|
) {
|
|
|
|
cursorIndex -= MarkdownSnippetMarker.SelectedCursor.length;
|
|
|
|
}
|
|
|
|
|
2023-11-24 18:53:19 +00:00
|
|
|
if (cursorIndex === -1) {
|
|
|
|
cursorIndex = 0;
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:24:08 +00:00
|
|
|
let cursorPosition = cursorIndex;
|
2023-11-24 18:53:19 +00:00
|
|
|
const snippetLength = markdown.length;
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
// If any text has been selected, include it.
|
|
|
|
if (selectionStart !== selectionEnd) {
|
|
|
|
markdown =
|
2023-11-21 11:24:08 +00:00
|
|
|
markdown.slice(0, cursorIndex) +
|
2020-10-10 23:32:27 +00:00
|
|
|
textarea.value.slice(selectionStart, selectionEnd) +
|
2023-11-21 11:24:08 +00:00
|
|
|
markdown.slice(cursorIndex);
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2023-11-21 11:24:08 +00:00
|
|
|
cursorPosition =
|
|
|
|
selectedCursorIndex === -1 ? cursorIndex : selectedCursorIndex;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
textarea.value =
|
|
|
|
textarea.value.slice(0, selectionStart) +
|
|
|
|
markdown +
|
|
|
|
textarea.value.slice(selectionEnd);
|
|
|
|
|
2023-11-24 18:53:19 +00:00
|
|
|
if (cursorPosition === 0) {
|
|
|
|
// If no <cursor> marker was used in the snippet, then put the cursor at the
|
|
|
|
// end of the snippet.
|
|
|
|
cursorPosition = snippetLength;
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:24:08 +00:00
|
|
|
textarea.selectionEnd = selectionEnd + cursorPosition;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|