2020-10-10 23:32:27 +00:00
|
|
|
import {html} from 'htm/preact';
|
|
|
|
import {render} from 'preact';
|
2022-02-23 13:52:06 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
markdown: '[<>]()',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Link',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: false,
|
|
|
|
markdown: '```\n<>\n```',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Code',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: false,
|
|
|
|
markdown: '~~<>~~',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Strikethrough',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: false,
|
|
|
|
markdown:
|
|
|
|
'<details>\n<summary>Click to expand spoiler.</summary>\n\n<>\n</details>',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Spoilerbox',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
|
|
|
markdown: '**<>**',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Bold',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
|
|
|
markdown: '\n\n---\n\n<>',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Horizontal Divider',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
|
|
|
markdown: '`<>`',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Inline Code',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
|
|
|
markdown: '*<>*',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Italic',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
|
|
|
markdown: '1. <>',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Ordered List',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
|
|
|
markdown: '<small><></small>',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Small',
|
2020-10-10 23:32:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dropdown: true,
|
|
|
|
markdown: '* <>',
|
2022-02-23 13:52:06 +00:00
|
|
|
name: 'Unordered List',
|
|
|
|
},
|
2020-10-10 23:32:27 +00:00
|
|
|
].map(({dropdown, markdown, name}) => ({
|
|
|
|
dropdown,
|
|
|
|
name,
|
|
|
|
index: markdown.indexOf('<>'),
|
2022-02-23 13:52:06 +00:00
|
|
|
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.
|
|
|
|
const markdownForms = querySelectorAll('.form-markdown:not(.trx-toolbar)');
|
|
|
|
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.
|
|
|
|
form.classList.add('trx-toolbar');
|
|
|
|
|
|
|
|
const menu = form.querySelector<HTMLElement>('.tab-markdown-mode')!;
|
|
|
|
const textarea = form.querySelector<HTMLElement>(
|
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)
|
|
|
|
.map(
|
|
|
|
(snippet) =>
|
2022-02-23 13:52:06 +00:00
|
|
|
html`<${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.
|
|
|
|
const menuPlaceholder = document.createElement('div');
|
|
|
|
menu.append(menuPlaceholder);
|
|
|
|
render(snippetButtons, menu, menuPlaceholder);
|
|
|
|
|
|
|
|
// And render the dropdown directly after the menu.
|
|
|
|
const dropdownPlaceholder = document.createElement('div');
|
|
|
|
const menuParent = menu.parentElement!;
|
|
|
|
menu.after(dropdownPlaceholder);
|
|
|
|
render(
|
|
|
|
html`<${snippetDropdown} textarea=${textarea} />`,
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
function snippetButton(props: Required<Props>): TRXComponent {
|
|
|
|
const click = (event: MouseEvent) => {
|
|
|
|
event.preventDefault();
|
|
|
|
insertSnippet(props);
|
|
|
|
};
|
|
|
|
|
2022-02-23 13:52:06 +00:00
|
|
|
return html`
|
|
|
|
<li class="tab-item">
|
|
|
|
<button class="btn btn-link" onClick="${click}">
|
|
|
|
${props.snippet.name}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
`;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function snippetDropdown(props: Props): TRXComponent {
|
|
|
|
const options = snippets.map(
|
2022-02-23 13:52:06 +00:00
|
|
|
(snippet) => html`<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;
|
|
|
|
};
|
|
|
|
|
2022-02-23 13:52:06 +00:00
|
|
|
return html`
|
|
|
|
<select class="form-select" onChange=${change}>
|
|
|
|
<option>More…</option>
|
|
|
|
${options}
|
|
|
|
</select>
|
|
|
|
`;
|
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)".
|
|
|
|
if (snippet.name === 'Link') {
|
|
|
|
index += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
textarea.value =
|
|
|
|
textarea.value.slice(0, selectionStart) +
|
|
|
|
markdown +
|
|
|
|
textarea.value.slice(selectionEnd);
|
|
|
|
|
|
|
|
textarea.selectionEnd = selectionEnd + index;
|
|
|
|
}
|