MilkDownEditor and PostMdViewer

master
Niko PLP 5 months ago
parent 0a5c03ebd1
commit f289170e74
  1. 13
      ng-app/package.json
  2. 147
      ng-app/src/apps/MilkDownEditor.svelte
  3. 94
      ng-app/src/apps/PostMdViewer.svelte
  4. 1
      ng-app/src/apps/ProseMirrorEditor.svelte
  5. 102
      ng-app/src/apps/milkdown-placeholder.ts
  6. 26
      ng-app/src/styles.css
  7. 6
      ng-app/vite.config.ts
  8. 1
      package.json
  9. 874
      pnpm-lock.yaml

@ -32,6 +32,16 @@
"@lezer/highlight": "^1.0.0",
"@lezer/javascript": "^1.2.0",
"@lezer/lr": "^1.0.0",
"@milkdown-lab/plugin-split-editing": "^1.2.3",
"@milkdown/core": "^7.4.0",
"@milkdown/ctx": "^7.2.0",
"@milkdown/plugin-collab": "^7.4.0",
"@milkdown/plugin-slash": "^7.4.0",
"@milkdown/preset-commonmark": "^7.4.0",
"@milkdown/preset-gfm": "^7.4.0",
"@milkdown/prose": "^7.2.0",
"@milkdown/theme-nord": "^7.4.0",
"@milkdown/transformer": "^7.2.0",
"@popperjs/core": "^2.11.8",
"@replit/codemirror-lang-svelte": "^6.0.0",
"@tauri-apps/api": "2.0.0-alpha.8",
@ -40,14 +50,17 @@
"async-proxy": "^0.4.1",
"classnames": "^2.3.2",
"codemirror": "^6.0.1",
"extend": "3.0.2",
"flowbite": "^1.6.5",
"flowbite-svelte": "^0.43.3",
"html5-qrcode": "^2.3.8",
"lodash.debounce": "4.0.8",
"ng-sdk-js": "workspace:^0.1.0-preview.1",
"prosemirror-model": "^1.7.1",
"prosemirror-state": "^1.2.3",
"prosemirror-svelte": "^0.2.4",
"prosemirror-view": "^1.9.10",
"style-mod": "^4.1.2",
"svelte-codemirror-editor": "^1.4.0",
"svelte-i18n": "^4.0.0",
"svelte-inview": "^4.0.2",

@ -0,0 +1,147 @@
<!--
// Copyright (c) 2022-2024 Niko Bonnieure, Par le Peuple, NextGraph.org developers
// All rights reserved.
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
-->
<!--
TODO:
https://github.com/Milkdown/milkdown/tree/main/packages/components/src
https://milkdown-storybook.vercel.app/?path=/story/components-image-block--empty
https://github.com/Milkdown/milkdown/tree/main/packages/crepe
https://milkdown-storybook.vercel.app/?path=/story/crepe-crepe--empty
-->
<script lang="ts">
import { onMount, tick, onDestroy } from "svelte";
import {
toast_error,
toast_success,
reset_toasts,
display_error,
live_discrete_update,
discrete_update
} from "../store";
import {
cur_tab_register_on_save,
cur_tab_deregister_on_save,
cur_tab_branch_class
} from "../tab";
import { t } from "svelte-i18n";
import * as Y from 'yjs'
import { Editor, rootCtx, editorViewCtx } from '@milkdown/core';
import { commonmark } from '@milkdown/preset-commonmark';
import { gfm } from '@milkdown/preset-gfm'
import { nord } from '@milkdown/theme-nord';
import '@milkdown/theme-nord/style.css';
import { collab, collabServiceCtx } from '@milkdown/plugin-collab';
import { placeholder, placeholderCtx } from './milkdown-placeholder'
import { splitEditing, toggleSplitEditing } from '@milkdown-lab/plugin-split-editing'
import { SlashProvider, slashFactory } from '@milkdown/plugin-slash'
export let commits = {};
const ydoc = new Y.Doc()
let editor;
function slashPluginView(view) {
const content = document.createElement('div');
const provider = new SlashProvider({
content,
});
return {
update: (updatedView, prevState) => {
provider.update(updatedView, prevState);
},
destroy: () => {
provider.destroy();
content.remove();
}
}
}
const slash = slashFactory('my-slash');
async function setup() {
editor = await Editor.make().config((ctx) => {
ctx.set(rootCtx, '#mdeditor')
ctx.set(placeholderCtx, $t("doc.type_your_text_here"))
ctx.set(slash.key, {
view: slashPluginView
})
}).use(slash).config(nord).use(commonmark).use(gfm).use(placeholder).use(splitEditing).use(collab).create();
ydoc.on('update', async (update, origin) => {
//console.log(update,origin);
if (!origin.local) {
try {
await discrete_update(update, "YXml", commits.heads);
} catch (e){
toast_error(display_error(e));
}
}
})
ydoc.on('destroy', async () => {
commits.discrete?.deregisterOnUpdate();
await cur_tab_deregister_on_save();
})
editor.action((ctx) => {
const collabService = ctx.get(collabServiceCtx);
collabService
// bind doc
.bindDoc(ydoc)
// connect yjs with milkdown
.connect();
});
cur_tab_register_on_save(async (updates)=>{
let update = Y.mergeUpdates(updates);
await live_discrete_update(update, "YXml", commits.heads);
});
let history = commits.discrete?.registerOnUpdate((update) => {
Y.applyUpdate(ydoc, update.YXml, {local:true})
});
for (const h of history) {
Y.applyUpdate(ydoc, h.YXml, {local:true})
}
await tick();
editor.action((ctx) => {
const editorView = ctx.get(editorViewCtx)
editorView.focus();
//toggleSplitEditing(ctx);
});
}
onMount(async ()=>{
await setup();
});
onDestroy(async ()=>{
ydoc.destroy();
if (editor) await editor.destroy();
});
</script>
<div class="grow p-5 post-rich-text" style="min-height:300px;">
<div id="mdeditor" class="prosemirror-editor"></div>
</div>

@ -0,0 +1,94 @@
<!--
// Copyright (c) 2022-2024 Niko Bonnieure, Par le Peuple, NextGraph.org developers
// All rights reserved.
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
-->
<!--
We could maybe also use https://ssssota.github.io/svelte-exmarkdown/ for rendering the MD (but to obtain the MD, we need to instantiate Milkdown anyway)
-->
<script lang="ts">
import { onMount, tick, onDestroy } from "svelte";
import {
toast_error,
toast_success,
reset_toasts,
display_error,
live_discrete_update,
discrete_update
} from "../store";
import {
cur_tab_register_on_save,
cur_tab_deregister_on_save,
cur_tab_branch_class
} from "../tab";
import { t } from "svelte-i18n";
import * as Y from 'yjs'
import { Editor, rootCtx, editorViewOptionsCtx } from '@milkdown/core';
import { commonmark } from '@milkdown/preset-commonmark';
import { gfm } from '@milkdown/preset-gfm'
import { nord } from '@milkdown/theme-nord';
import '@milkdown/theme-nord/style.css';
import { collab, collabServiceCtx } from '@milkdown/plugin-collab';
export let commits = {};
const ydoc = new Y.Doc()
let editor;
async function setup() {
editor = await Editor.make().config((ctx) => {
ctx.set(rootCtx, '#mdeditor')
ctx.update(editorViewOptionsCtx, (prev) => ({
...prev,
editable:() => false,
}))
}).config(nord).use(commonmark).use(gfm).use(collab).create();
ydoc.on('destroy', async () => {
commits.discrete?.deregisterOnUpdate();
})
editor.action((ctx) => {
const collabService = ctx.get(collabServiceCtx);
collabService
// bind doc and awareness
.bindDoc(ydoc)
// connect yjs with milkdown
.connect();
});
let history = commits.discrete?.registerOnUpdate((update) => {
Y.applyUpdate(ydoc, update.YXml, {local:true})
});
for (const h of history) {
Y.applyUpdate(ydoc, h.YXml, {local:true})
}
}
onMount(async ()=>{
await setup();
});
onDestroy(async ()=>{
ydoc.destroy();
await editor.destroy();
});
</script>
<div class="grow p-5 post-rich-text">
<div id="mdeditor" class="prosemirror-editor"></div>
</div>

@ -42,7 +42,6 @@
let view;
ydoc.on('update', async (update, origin) => {
console.log(update,origin);
if (!origin.local) {
try {
await discrete_update(update, "YXml", commits.heads);

@ -0,0 +1,102 @@
/**
MIT License
Copyright (c) 2022 Mox
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Source: https://github.com/HexMox/milkdown-plugin-placeholder
*/
import type { MilkdownPlugin, TimerType } from '@milkdown/ctx'
import type { EditorView } from '@milkdown/prose/view'
import { createSlice, createTimer } from '@milkdown/ctx'
import { InitReady, prosePluginsCtx } from '@milkdown/core'
import { Plugin, PluginKey } from '@milkdown/prose/state'
export const placeholderCtx = createSlice('Please input here...', 'placeholder')
export const placeholderTimerCtx = createSlice([] as TimerType[], 'editorStateTimer')
export const PlaceholderReady = createTimer('PlaceholderReady')
const key = new PluginKey('MILKDOWN_PLACEHOLDER')
export const placeholder: MilkdownPlugin = (ctx) => {
ctx.inject(placeholderCtx).inject(placeholderTimerCtx, [InitReady]).record(PlaceholderReady)
return async () => {
await ctx.waitTimers(placeholderTimerCtx)
const prosePlugins = ctx.get(prosePluginsCtx)
const update = (view: EditorView) => {
const placeholder = ctx.get(placeholderCtx)
const doc = view.state.doc
if (
view.editable &&
doc.childCount === 1 &&
doc.firstChild?.isTextblock &&
doc.firstChild?.content.size === 0 &&
doc.firstChild?.type.name === 'paragraph'
) {
view.dom.classList.add('editor_empty');
view.dom.setAttribute('data-placeholder', placeholder);
} else {
view.dom.classList.remove('editor_empty');
}
}
const plugins = [
...prosePlugins,
new Plugin({
key,
// props: {
// decorations(state) {
// const doc = state.doc
// if (
// doc.childCount === 1 &&
// doc.firstChild?.isTextblock &&
// doc.firstChild?.content.size === 0
// ) {
// return DecorationSet.create(doc, [
// Decoration.widget(1, (view) => {
// if (view.editable) {
// const span = document.createElement('span')
// span.classList.add('placeholder')
// span.textContent = placeholder
// return span
// }
// }),
// ])
// }
// },
// },
view(view) {
update(view)
return { update }
},
}),
]
ctx.set(prosePluginsCtx, plugins)
ctx.done(PlaceholderReady)
}
}

@ -26,7 +26,31 @@ td.hljs {
.splashing {
display: none;
}
.prosemirror-editor {
.split-editor {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: 1fr;
}
.split-editor:has(.milkdown-split-editor.hidden) {
grid-template-columns: repeat(1, 1fr);
}
.milkdown-split-editor.hidden {
display: none;
}
.ProseMirror.editor_empty::before {
position: absolute;
content: attr(data-placeholder);
pointer-events: none;
color: rgb(170, 170, 170);
}
.prosemirror-editor,
.prosemirror-editor .milkdown,
.prosemirror-editor .milkdown .ProseMirror {
height: 100%;
overflow-wrap: anywhere !important;
}

@ -14,7 +14,11 @@ export default defineConfig(async () => {
optimizeDeps: {
exclude: ["codemirror", "@codemirror/*", "@codemirror/language", "@codemirror/state", "@codemirror/view","@codemirror/legacy-modes/mode/sparql",
"@codemirror/lang-javascript", "@codemirror/lang-rust", "@replit/codemirror-lang-svelte", "yjs", "y-codemirror.next", "svelte-codemirror-editor",
"prosemirror-svelte", "prosemirror-svelte/state", "prosemirror-svelte/helpers", "y-prosemirror", "prosemirror-state", "prosemirror-model", "prosemirror-view", "y-protocols"],
"prosemirror-svelte", "prosemirror-svelte/state", "prosemirror-svelte/helpers", "y-prosemirror", "prosemirror-state", "prosemirror-model", "prosemirror-view", "y-protocols",
"@milkdown/core", "@milkdown/ctx", "@milkdown/prose", "@milkdown/transformer", "@milkdown/preset-commonmark", "@milkdown/theme-nord", "@milkdown/plugin-collab",
"svelte-highlight", "svelte-highlight/languages/typescript", "svelte-highlight/languages/javascript", "svelte-highlight/languages/rust", "@milkdown/preset-gfm",
"@milkdown-lab/plugin-split-editing", "@milkdown/plugin-slash"],
include: ["debug","extend","highlight.js","highlight.js/lib/core","lodash.debounce"]
},
worker: {
format: 'es',

@ -14,5 +14,6 @@
"prettier-plugin-svelte": "^3.2.5"
},
"dependencies": {
"style-mod": "^4.1.2"
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save