2023-06-23 10:52:03 +00:00
|
|
|
import {render} from "preact";
|
|
|
|
import {log, querySelectorAll} from "../../utilities/exports.js";
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
type MarkdownSnippet = {
|
|
|
|
dropdown: boolean;
|
|
|
|
index: number;
|
|
|
|
markdown: string;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const snippets: MarkdownSnippet[] = [
|
|
|
|
{
|
|
|
|
dropdown: false,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "[<>]()",
|
|
|
|
name: "Link",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: false,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "```\n<>\n```",
|
|
|
|
name: "Code",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: false,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "~~<>~~",
|
|
|
|
name: "Strikethrough",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: false,
|
|
|
|
markdown:
|
2023-06-23 10:52:03 +00:00
|
|
|
"<details>\n<summary>Click to expand spoiler.</summary>\n\n<>\n</details>",
|
|
|
|
name: "Spoilerbox",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "**<>**",
|
|
|
|
name: "Bold",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "\n\n---\n\n<>",
|
|
|
|
name: "Horizontal Divider",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "`<>`",
|
|
|
|
name: "Inline Code",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "*<>*",
|
|
|
|
name: "Italic",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "1. <>",
|
|
|
|
name: "Ordered List",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "<small><></small>",
|
|
|
|
name: "Small",
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
2023-06-23 10:52:03 +00:00
|
|
|
markdown: "* <>",
|
|
|
|
name: "Unordered List",
|
2022-02-23 13:52:06 +00:00
|
|
|
},
|
2020-10-10 23:32:27 +00:00
|
|
|
].map(({dropdown, markdown, name}) => ({
|
|
|
|
dropdown,
|
|
|
|
name,
|
2023-06-23 10:52:03 +00:00
|
|
|
index: markdown.indexOf("<>"),
|
|
|
|
markdown: markdown.replace(/<>/, ""),
|
2020-10-10 23:32:27 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
export function runMarkdownToolbarFeature() {
|
2022-02-23 22:41:32 +00:00
|
|
|
const count = addToolbarsToTextareas();
|
|
|
|
log(`Markdown Toolbar: Initialized for ${count} textareas.`);
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 22:41:32 +00:00
|
|
|
function addToolbarsToTextareas(): 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
|
|
|
|
.filter((snippet) => !snippet.dropdown)
|
2023-06-23 10:52:03 +00:00
|
|
|
.map((snippet) => (
|
|
|
|
<SnippetButton snippet={snippet} textarea={textarea} />
|
|
|
|
));
|
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);
|
|
|
|
|
|
|
|
// And render the dropdown directly after the menu.
|
2023-06-23 10:52:03 +00:00
|
|
|
const dropdownPlaceholder = document.createElement("div");
|
2020-10-10 23:32:27 +00:00
|
|
|
const menuParent = menu.parentElement!;
|
|
|
|
menu.after(dropdownPlaceholder);
|
|
|
|
render(
|
2023-06-23 10:52:03 +00:00
|
|
|
<SnippetDropdown textarea={textarea} />,
|
2020-10-10 23:32:27 +00:00
|
|
|
menuParent,
|
2022-02-23 13:52:06 +00:00
|
|
|
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 = {
|
|
|
|
snippet?: MarkdownSnippet;
|
|
|
|
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}>
|
|
|
|
{props.snippet.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) {
|
|
|
|
const options = snippets.map((snippet) => (
|
|
|
|
<option value={snippet.name}>{snippet.name}</option>
|
|
|
|
));
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
const change = (event: Event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const snippet = snippets.find(
|
2022-02-23 13:52:06 +00:00
|
|
|
(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();
|
|
|
|
|
|
|
|
let {index, markdown} = snippet;
|
|
|
|
|
|
|
|
// If any text has been selected, include it.
|
|
|
|
if (selectionStart !== selectionEnd) {
|
|
|
|
markdown =
|
|
|
|
markdown.slice(0, index) +
|
|
|
|
textarea.value.slice(selectionStart, selectionEnd) +
|
|
|
|
markdown.slice(index);
|
|
|
|
|
|
|
|
// Change the index when the Link snippet is used so the cursor ends up
|
|
|
|
// in the URL part of the Markdown: "[existing text](cursor here)".
|
2023-06-23 10:52:03 +00:00
|
|
|
if (snippet.name === "Link") {
|
2020-10-10 23:32:27 +00:00
|
|
|
index += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
textarea.value =
|
|
|
|
textarea.value.slice(0, selectionStart) +
|
|
|
|
markdown +
|
|
|
|
textarea.value.slice(selectionEnd);
|
|
|
|
|
|
|
|
textarea.selectionEnd = selectionEnd + index;
|
|
|
|
}
|