feat: add markdown toolbar
This commit is contained in:
1 parent
e75c674cb8
commit
3dcf173826
9 files changed
+192
-19
No files matched your search
@@ -44,6 +44,9 @@
|
||||
sm: 'px-2 py-1',
|
||||
md: 'px-3 py-1.5',
|
||||
lg: 'px-4 py-2',
|
||||
'square-sm': 'w-6 h-6',
|
||||
'square-md': 'w-8 h-8',
|
||||
'square-lg': 'w-10 h-10',
|
||||
}
|
||||
|
||||
const buttonRoundness = {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
export let maxlength: number = -1
|
||||
export let rows: number = 3
|
||||
export let required: boolean = false
|
||||
export let item: any = undefined
|
||||
|
||||
let clazz = ''
|
||||
export { clazz as class }
|
||||
@@ -27,6 +28,7 @@
|
||||
{/if}
|
||||
<textarea
|
||||
bind:value
|
||||
bind:this={item}
|
||||
on:input={(e) => dispatcher('input', e)}
|
||||
{placeholder}
|
||||
{maxlength}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { toast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
|
||||
|
||||
export let postId: number
|
||||
export let parentId: number | undefined = undefined
|
||||
@@ -65,10 +66,9 @@
|
||||
<Markdown source={value} />
|
||||
</div>
|
||||
{:else}
|
||||
<TextArea
|
||||
<MarkdownEditor
|
||||
rows={4}
|
||||
placeholder={locked ? 'This post is locked.' : 'What are you thinking?'}
|
||||
class="!bg-slate-100 dark:!bg-zinc-900"
|
||||
bind:value
|
||||
disabled={locked}
|
||||
/>
|
||||
|
||||
@@ -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