feat: add markdown toolbar
This commit is contained in:
1 parent
e75c674cb8
commit
3dcf173826
9 files changed
+192
-19
No files matched your search
@@ -22,6 +22,8 @@
|
||||
"autoprefixer": "^10.4.14",
|
||||
"jsdom": "^22.1.0",
|
||||
"markdown-it-container": "^3.0.0",
|
||||
"markdown-it-sub": "^1.0.0",
|
||||
"markdown-it-sup": "^1.0.0",
|
||||
"nprogress": "^0.2.0",
|
||||
"postcss": "^8.4.24",
|
||||
"svelte": "^3.54.0",
|
||||
|
||||
Vendored
+3
@@ -14,3 +14,6 @@ declare global {
|
||||
|
||||
export {}
|
||||
declare const __VERSION__: string
|
||||
|
||||
declare module 'markdown-it-sub'
|
||||
declare module 'markdown-it-sup'
|
||||
@@ -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) {
|
||||
|
||||
+2
-1
@@ -3,7 +3,8 @@ export default {
|
||||
content: ['src/**/*.{js,ts,svelte,html}'],
|
||||
theme: {
|
||||
fontFamily: {
|
||||
'sans': ['Cantarell', 'system-ui']
|
||||
'sans': ['Cantarell', 'system-ui'],
|
||||
'serif': ['serif']
|
||||
},
|
||||
extend: {
|
||||
animation: {
|
||||
|
||||
@@ -2789,6 +2789,16 @@ markdown-it-container@^3.0.0:
|
||||
resolved "https://registry.yarnpkg.com/markdown-it-container/-/markdown-it-container-3.0.0.tgz#1d19b06040a020f9a827577bb7dbf67aa5de9a5b"
|
||||
integrity sha512-y6oKTq4BB9OQuY/KLfk/O3ysFhB3IMYoIWhGJEidXt1NQFocFK2sA2t0NYZAMyMShAGL6x5OPIbrmXPIqaN9rw==
|
||||
|
||||
markdown-it-sub@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/markdown-it-sub/-/markdown-it-sub-1.0.0.tgz#375fd6026eae7ddcb012497f6411195ea1e3afe8"
|
||||
integrity sha512-z2Rm/LzEE1wzwTSDrI+FlPEveAAbgdAdPhdWarq/ZGJrGW/uCQbKAnhoCsE4hAbc3SEym26+W2z/VQB0cQiA9Q==
|
||||
|
||||
markdown-it-sup@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/markdown-it-sup/-/markdown-it-sup-1.0.0.tgz#cb9c9ff91a5255ac08f3fd3d63286e15df0a1fc3"
|
||||
integrity sha512-E32m0nV9iyhRR7CrhnzL5msqic7rL1juWre6TQNxsnApg7Uf+F97JOKxUijg5YwXz86lZ0mqfOnutoryyNdntQ==
|
||||
|
||||
markdown-it@^13.0.1:
|
||||
version "13.0.1"
|
||||
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-13.0.1.tgz#c6ecc431cacf1a5da531423fc6a42807814af430"
|
||||
|
||||
Reference in new issue
Block a user