Repository navigation
feat(editor): add lowlight code blocks #3199
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a6333a
feat(editor): add code block lowlight with theme-aware highlighting
AlexAndBear af6358c
refactor(editor): move hljs theme switching to code block component
AlexAndBear 79db1de
refactor(editor): move code block and image handlers to extensions
AlexAndBear c3f00fd
fix(editor): disable code block language select in readonly mode
AlexAndBear b38fbae
refactor(editor): move hljs theme handling to text editor content
AlexAndBear ef7f984
enhance lang select
AlexAndBear 78f176e
align tip tap packages
AlexAndBear 4029b93
format
AlexAndBear b780bce
add single tick styling
AlexAndBear a974cc1
simplify theme loading
AlexAndBear 04bf18b
dedupe
AlexAndBear 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
packages/web-pkg/src/editor/components/CodeBlockComponent.vue
This file contains hidden or 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,80 @@ | ||
| <template> | ||
| <node-view-wrapper class="code-block"> | ||
| <div class="text-editor-code-block-language" contenteditable="false"> | ||
| <oc-select | ||
| class="text-editor-code-block-select" | ||
| :model-value="selectedLanguageOption" | ||
| :options="languageOptions" | ||
| :disabled="isReadonly" | ||
| option-label="label" | ||
| :clearable="false" | ||
| :searchable="true" | ||
| :label="$gettext('Code language')" | ||
| :label-hidden="true" | ||
| @update:model-value="updateSelectedLanguage" | ||
| /> | ||
| </div> | ||
| <pre><code><node-view-content /></code></pre> | ||
| </node-view-wrapper> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { computed } from 'vue' | ||
| import { NodeViewContent, nodeViewProps, NodeViewWrapper } from '@tiptap/vue-3' | ||
| import { useGettext } from 'vue3-gettext' | ||
|
|
||
| const props = defineProps(nodeViewProps) | ||
| const { $gettext } = useGettext() | ||
|
|
||
| type LanguageOption = { | ||
| label: string | ||
| value: string | null | ||
| } | ||
|
|
||
| const languages = computed<string[]>(() => { | ||
| return props.extension.options.lowlight.listLanguages() as string[] | ||
| }) | ||
|
|
||
| const selectedLanguage = computed<string | null>({ | ||
| get() { | ||
| return (props.node.attrs.language as string | null) ?? null | ||
| }, | ||
| set(language) { | ||
| props.updateAttributes({ language }) | ||
| } | ||
| }) | ||
|
|
||
| const languageOptions = computed<LanguageOption[]>(() => { | ||
| return [ | ||
| { label: 'auto', value: null }, | ||
| ...languages.value.map((language) => ({ label: language, value: language })) | ||
| ] | ||
| }) | ||
|
|
||
| const selectedLanguageOption = computed<LanguageOption>(() => { | ||
| const current = selectedLanguage.value | ||
| return languageOptions.value.find(({ value }) => value === current) ?? languageOptions.value[0] | ||
| }) | ||
|
|
||
| const isReadonly = computed(() => props.editor.isEditable === false) | ||
|
|
||
| function updateSelectedLanguage(option: LanguageOption | null) { | ||
| selectedLanguage.value = option?.value ?? null | ||
| } | ||
| </script> | ||
|
|
||
| <style scoped> | ||
| .text-editor-code-block-language { | ||
| position: absolute; | ||
| right: 12px; | ||
| top: 4px; | ||
| min-width: 130px; | ||
| max-width: calc(100% - 24px); | ||
| } | ||
|
|
||
| .text-editor-code-block-select :deep(.vs__dropdown-toggle) { | ||
| min-height: 30px !important; | ||
| padding-top: 0 !important; | ||
| padding-bottom: 0 !important; | ||
| } | ||
| </style> |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
14 changes: 14 additions & 0 deletions
14
packages/web-pkg/src/editor/extensions/codeBlockLowlight.ts
This file contains hidden or 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,14 @@ | ||
| import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight' | ||
| import { VueNodeViewRenderer } from '@tiptap/vue-3' | ||
| import { common, createLowlight } from 'lowlight' | ||
| import CodeBlockComponent from '../components/CodeBlockComponent.vue' | ||
|
|
||
| const lowlight = createLowlight(common) | ||
|
|
||
| export function createCodeBlockLowlight() { | ||
| return CodeBlockLowlight.extend({ | ||
| addNodeView() { | ||
| return VueNodeViewRenderer(CodeBlockComponent) | ||
| } | ||
| }).configure({ lowlight }) | ||
| } |
File renamed without changes.
This file contains hidden or 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,2 +1,4 @@ | ||
| export * from './codeBlockLowlight' | ||
| export * from './imageFileHandler' | ||
| export * from './link' | ||
| export * from './slashCommands' |
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.
good that you moved it 👍