feat: add markdown toolbar
This commit is contained in:
1 parent
e75c674cb8
commit
3dcf173826
9 files changed
+192
-19
No files matched your search
@@ -0,0 +1,143 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import TextArea from '$lib/components/input/TextArea.svelte'
|
||||
import {
|
||||
CodeBracket,
|
||||
ExclamationTriangle,
|
||||
Icon,
|
||||
Link,
|
||||
ListBullet,
|
||||
} from 'svelte-hero-icons'
|
||||
|
||||
export let images: boolean = true
|
||||
export let value: string = ''
|
||||
|
||||
let textArea: HTMLTextAreaElement
|
||||
|
||||
function replaceTextAtIndices(
|
||||
str: string,
|
||||
startIndex: number,
|
||||
endIndex: number,
|
||||
replacement: string
|
||||
) {
|
||||
return str.substring(0, startIndex) + replacement + str.substring(endIndex)
|
||||
}
|
||||
|
||||
function wrapSelection(start: string, end: string) {
|
||||
const startPos = textArea.selectionStart
|
||||
const endPos = textArea.selectionEnd
|
||||
|
||||
const substring = textArea.value.substring(startPos, endPos)
|
||||
let newText = `${start}${substring}${end}`
|
||||
|
||||
textArea.value = replaceTextAtIndices(
|
||||
textArea.value,
|
||||
startPos,
|
||||
endPos,
|
||||
newText
|
||||
)
|
||||
|
||||
textArea.focus()
|
||||
textArea.selectionStart = startPos + start.length
|
||||
textArea.selectionEnd = endPos + start.length
|
||||
}
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="flex flex-col border border-slate-200 dark:border-zinc-800 rounded-md
|
||||
overflow-hidden"
|
||||
>
|
||||
<!--Toolbar-->
|
||||
<div class="flex flex-row overflow-auto p-1.5 gap-1.5">
|
||||
<Button
|
||||
on:click={() => wrapSelection('**', '**')}
|
||||
title="Bold"
|
||||
size="square-md"
|
||||
>
|
||||
<span class="font-bold">B</span>
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('*', '*')}
|
||||
title="Italic"
|
||||
size="square-md"
|
||||
>
|
||||
<span class="italic font-bold">I</span>
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('[label](url)', '')}
|
||||
title="Link"
|
||||
size="square-md"
|
||||
>
|
||||
<Icon src={Link} mini size="16" />
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('\n# ', '')}
|
||||
title="Header"
|
||||
size="square-md"
|
||||
>
|
||||
<span class="italic font-bold font-serif">H</span>
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('~~', '~~')}
|
||||
title="Strikethrough"
|
||||
size="square-md"
|
||||
>
|
||||
<span class="line-through font-bold">S</span>
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('\n> ', '')}
|
||||
title="Quote"
|
||||
size="square-md"
|
||||
>
|
||||
<span class="font-bold font-serif">"</span>
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('\n- ', '')}
|
||||
title="List"
|
||||
size="square-md"
|
||||
>
|
||||
<Icon src={ListBullet} mini size="16" />
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('`', '`')}
|
||||
title="Code"
|
||||
size="square-md"
|
||||
>
|
||||
<Icon src={CodeBracket} mini size="16" />
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('::: spoiler <spoiler title>\n', '\n:::')}
|
||||
title="Spoiler"
|
||||
size="square-md"
|
||||
>
|
||||
<Icon src={ExclamationTriangle} mini size="16" />
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('~', '~')}
|
||||
title="Subscript"
|
||||
size="square-md"
|
||||
>
|
||||
<span class="font-bold">
|
||||
X
|
||||
<sub>1</sub>
|
||||
</span>
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => wrapSelection('^', '^')}
|
||||
title="Superscript"
|
||||
size="square-md"
|
||||
>
|
||||
<span class="font-bold">
|
||||
X
|
||||
<sup>1</sup>
|
||||
</span>
|
||||
</Button>
|
||||
</div>
|
||||
<!--Actual text area-->
|
||||
<TextArea
|
||||
class="bg-inherit border-0 rounded-none"
|
||||
bind:value
|
||||
bind:item={textArea}
|
||||
{...$$restProps}
|
||||
/>
|
||||
</div>
|
||||
@@ -1,26 +1,33 @@
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import markdown_it_container from 'markdown-it-container'
|
||||
// @ts-ignore
|
||||
import markdown_it_sub from 'markdown-it-sub'
|
||||
// @ts-ignore
|
||||
import markdown_it_sup from 'markdown-it-sup'
|
||||
|
||||
export const md = new MarkdownIt({
|
||||
html: false,
|
||||
linkify: true,
|
||||
}).use(markdown_it_container, 'spoiler', {
|
||||
validate: (params: string) => {
|
||||
return params.trim().match(/^spoiler+(.*)/)
|
||||
},
|
||||
|
||||
render: (tokens: any, idx: any) => {
|
||||
var m = tokens[idx].info.trim().match(/^spoiler+(.*)/)
|
||||
|
||||
if (tokens[idx].nesting === 1) {
|
||||
// opening tag
|
||||
return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`
|
||||
} else {
|
||||
// closing tag
|
||||
return '</details>\n'
|
||||
}
|
||||
},
|
||||
})
|
||||
.use(markdown_it_container, 'spoiler', {
|
||||
validate: (params: string) => {
|
||||
return params.trim().match(/^spoiler+(.*)/)
|
||||
},
|
||||
|
||||
render: (tokens: any, idx: any) => {
|
||||
var m = tokens[idx].info.trim().match(/^spoiler+(.*)/)
|
||||
|
||||
if (tokens[idx].nesting === 1) {
|
||||
// opening tag
|
||||
return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`
|
||||
} else {
|
||||
// closing tag
|
||||
return '</details>\n'
|
||||
}
|
||||
},
|
||||
})
|
||||
.use(markdown_it_sub)
|
||||
.use(markdown_it_sup)
|
||||
|
||||
export const mdInline: MarkdownIt = new MarkdownIt('zero')
|
||||
.enable(['emphasis', 'backticks', 'strikethrough', 'link'])
|
||||
@@ -41,6 +48,8 @@ export const mdInline: MarkdownIt = new MarkdownIt('zero')
|
||||
}
|
||||
},
|
||||
})
|
||||
.use(markdown_it_sub)
|
||||
.use(markdown_it_sup)
|
||||
|
||||
md.linkify.add('!', {
|
||||
validate: function (text, pos, self) {
|
||||
|
||||
Reference in new issue
Block a user