-
Notifications
You must be signed in to change notification settings - Fork 328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CodeMirror implementation of GraphNodeComment #11585
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
62d8bdd
Render tables in documentation.
kazcw 40875ef
CHANGELOG, prettier
kazcw 6bd2b95
Apply @farmaazon review.
kazcw c6432d0
Fix
kazcw be9bdd3
Lint
kazcw 3d75871
Cleanup
kazcw e654dfd
Integration tests for GraphNodeComment
kazcw f317637
Merge remote-tracking branch 'origin/develop' into wip/kw/doc-tables
kazcw 7431384
Workaround stuck CI
kazcw 58adff3
Merge branch 'wip/kw/node-comment-tests' into staging
kazcw 621be55
Merge branch 'wip/kw/doc-tables' into staging
kazcw 89cf8df
CodeMirror implementation of GraphNodeComment
kazcw caaa01a
Revert "Workaround stuck CI"
kazcw 4606f2b
Merge branch 'develop' into wip/kw/node-comment-tests
mergify[bot] 4f8c80f
Merge branch 'develop' into wip/kw/doc-tables
mergify[bot] fcac2fd
Revert "Workaround stuck CI"
kazcw ae2b93f
Merge remote-tracking branch 'origin/wip/kw/node-comment-tests' into …
kazcw c58c8b7
Merge remote-tracking branch 'origin/develop' into wip/kw/doc-tables
kazcw fb396d8
Merge branch 'wip/kw/doc-tables' into wip/kw/cm-comments
kazcw b7549bc
Merge remote-tracking branch 'origin/develop' into wip/kw/doc-tables
kazcw f11dcce
Fix merge
kazcw 2583a10
Merge branch 'wip/kw/doc-tables' into wip/kw/cm-comments
kazcw e80de7b
LINKABLE_URL_REGEX: Remove a Lexical special case
kazcw 373c56c
Merge remote-tracking branch 'origin/develop' into wip/kw/cm-comments
kazcw d9f2c02
Space cases
kazcw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,28 @@ test.each([ | |
}, | ||
], | ||
}, | ||
{ | ||
markdown: '[Link text](<https://www.example.com/index.html>)', | ||
expectedLinks: [ | ||
{ | ||
text: 'Link text', | ||
href: 'https://www.example.com/index.html', | ||
}, | ||
], | ||
}, | ||
Comment on lines
+67
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may consider adding cases for:
|
||
{ | ||
markdown: '[Link text](<https://www.example.com/Url with spaces.html>)', | ||
expectedLinks: [ | ||
{ | ||
text: 'Link text', | ||
href: 'https://www.example.com/Url with spaces.html', | ||
}, | ||
], | ||
}, | ||
{ | ||
markdown: '[Link text](https://www.example.com/Spaces not allowed without angle brackets.html)', | ||
expectedLinks: [], | ||
}, | ||
{ | ||
markdown: '[Unclosed url](https://www.example.com/index.html', | ||
expectedLinks: [], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,22 @@ | ||
<script setup lang="ts"> | ||
import FloatingSelectionMenu from '@/components/FloatingSelectionMenu.vue' | ||
import { lexicalTheme, useLexical, type LexicalPlugin } from '@/components/lexical' | ||
import LexicalContent from '@/components/lexical/LexicalContent.vue' | ||
import { autoLinkPlugin, useLinkNode } from '@/components/lexical/LinkPlugin' | ||
import LinkToolbar from '@/components/lexical/LinkToolbar.vue' | ||
import { useLexicalStringSync } from '@/components/lexical/sync' | ||
import { registerPlainText } from '@lexical/plain-text' | ||
import { ref, useCssModule, watch, type ComponentInstance } from 'vue' | ||
import { type ComponentInstance, computed, defineAsyncComponent, ref } from 'vue' | ||
import * as Y from 'yjs' | ||
|
||
const text = defineModel<string>({ required: true }) | ||
const props = defineProps<{ content: Y.Text | string }>() | ||
|
||
const contentElement = ref<ComponentInstance<typeof LexicalContent>>() | ||
const impl = ref<ComponentInstance<typeof LazyPlainTextEditor>>() | ||
|
||
const plainText: LexicalPlugin = { | ||
register: registerPlainText, | ||
} | ||
const LazyPlainTextEditor = defineAsyncComponent( | ||
() => import('@/components/PlainTextEditor/PlainTextEditorImpl.vue'), | ||
) | ||
|
||
const textSync: LexicalPlugin = { | ||
register: (editor) => { | ||
const { content } = useLexicalStringSync(editor) | ||
watch(text, (newContent) => content.set(newContent), { immediate: true }) | ||
watch(content.editedContent, (newContent) => (text.value = newContent)) | ||
}, | ||
} | ||
|
||
const theme = lexicalTheme(useCssModule('lexicalTheme')) | ||
const { editor } = useLexical(contentElement, 'PlainTextEditor', theme, [ | ||
autoLinkPlugin, | ||
plainText, | ||
textSync, | ||
]) | ||
const { urlUnderCursor } = useLinkNode(editor) | ||
|
||
defineExpose({ contentElement }) | ||
defineExpose({ | ||
contentElement: computed(() => impl.value?.contentElement), | ||
}) | ||
</script> | ||
|
||
<template> | ||
<LexicalContent ref="contentElement" v-bind="$attrs" /> | ||
<FloatingSelectionMenu :selectionElement="contentElement"> | ||
<LinkToolbar v-if="urlUnderCursor" :url="urlUnderCursor" /> | ||
</FloatingSelectionMenu> | ||
<Suspense> | ||
<LazyPlainTextEditor ref="impl" v-bind="props" class="PlainTextEditor" /> | ||
</Suspense> | ||
</template> | ||
|
||
<style module="lexicalTheme"> | ||
.link { | ||
color: #ddf; | ||
&:hover { | ||
text-decoration: underline; | ||
} | ||
} | ||
</style> |
45 changes: 45 additions & 0 deletions
45
app/gui/src/project-view/components/PlainTextEditor/PlainTextEditorImpl.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<script setup lang="ts"> | ||
import EditorRoot from '@/components/codemirror/EditorRoot.vue' | ||
import { yCollab } from '@/components/codemirror/yCollab' | ||
import { linkifyUrls } from '@/components/PlainTextEditor/linkifyUrls' | ||
import { EditorState } from '@codemirror/state' | ||
import { EditorView } from '@codemirror/view' | ||
import { type ComponentInstance, computed, onMounted, ref, watchEffect } from 'vue' | ||
import { Awareness } from 'y-protocols/awareness' | ||
import { assert } from 'ydoc-shared/util/assert' | ||
import * as Y from 'yjs' | ||
|
||
const { content } = defineProps<{ content: Y.Text | string }>() | ||
|
||
const editorRoot = ref<ComponentInstance<typeof EditorRoot>>() | ||
const awareness = new Awareness(new Y.Doc()) | ||
const editorView = new EditorView() | ||
|
||
function init(content: Y.Text | string) { | ||
const baseExtensions = [linkifyUrls] | ||
if (typeof content === 'string') { | ||
return { doc: content, extensions: baseExtensions } | ||
} else { | ||
assert(content.doc !== null) | ||
const yTextWithDoc: Y.Text & { doc: Y.Doc } = content as any | ||
const doc = content.toString() | ||
const syncExt = yCollab(yTextWithDoc, awareness) | ||
return { doc, extensions: [...baseExtensions, syncExt] } | ||
} | ||
} | ||
|
||
watchEffect(() => { | ||
const { doc, extensions } = init(content) | ||
editorView.setState(EditorState.create({ doc, extensions })) | ||
}) | ||
|
||
onMounted(() => editorRoot.value?.rootElement?.prepend(editorView.dom)) | ||
|
||
defineExpose({ | ||
contentElement: computed(() => editorView.contentDOM), | ||
}) | ||
</script> | ||
|
||
<template> | ||
<EditorRoot ref="editorRoot" /> | ||
</template> |
73 changes: 73 additions & 0 deletions
73
app/gui/src/project-view/components/PlainTextEditor/___tests__/urlLinks.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { linkifyUrls } from '@/components/PlainTextEditor/linkifyUrls' | ||
import { EditorState } from '@codemirror/state' | ||
import { Decoration, EditorView } from '@codemirror/view' | ||
import { expect, test } from 'vitest' | ||
|
||
function decorations<T>( | ||
source: string, | ||
recognize: (from: number, to: number, decoration: Decoration) => T | undefined, | ||
) { | ||
const state = EditorState.create({ | ||
doc: source, | ||
extensions: [linkifyUrls], | ||
}) | ||
const view = new EditorView({ state }) | ||
const decorationSets = state.facet(EditorView.decorations) | ||
const results = [] | ||
for (const decorationSet of decorationSets) { | ||
const resolvedDecorations = | ||
decorationSet instanceof Function ? decorationSet(view) : decorationSet | ||
const cursor = resolvedDecorations.iter() | ||
while (cursor.value != null) { | ||
const recognized = recognize(cursor.from, cursor.to, cursor.value) | ||
if (recognized) results.push(recognized) | ||
cursor.next() | ||
} | ||
} | ||
return results | ||
} | ||
|
||
function links(source: string) { | ||
return decorations(source, (from, to, deco) => { | ||
if (deco.spec.tagName === 'a') { | ||
return { | ||
text: source.substring(from, to), | ||
href: deco.spec.attributes.href, | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// Test that link decorations are created for URLs and emails, with `href` set appropriately. The specific URL and email | ||
// syntaxes recognized are tested separately, in the unit tests for `LINKABLE_URL_REGEX` and `LINKABLE_EMAIL_REGEX`. | ||
test.each([ | ||
{ | ||
text: 'Url: https://www.example.com/index.html', | ||
expectedLinks: [ | ||
{ | ||
text: 'https://www.example.com/index.html', | ||
href: 'https://www.example.com/index.html', | ||
}, | ||
], | ||
}, | ||
{ | ||
text: 'Url: www.example.com', | ||
expectedLinks: [ | ||
{ | ||
text: 'www.example.com', | ||
href: 'https://www.example.com', | ||
}, | ||
], | ||
}, | ||
{ | ||
text: 'Email: [email protected]', | ||
expectedLinks: [ | ||
{ | ||
text: '[email protected]', | ||
href: 'mailto:[email protected]', | ||
}, | ||
], | ||
}, | ||
])('Link decoration: $text', ({ text, expectedLinks }) => { | ||
expect(links(text)).toEqual(expectedLinks) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we turn this into a json? We expect a md string to parse it and display
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
toJSON
method ofY.Text
returns the unformatted text content as astring
.