mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* feat(builder): support custom HTML in the document head * fix(builder): place head HTML below custom JavaScript * fix(builder): autosave custom head HTML * fix(builder): remove redundant head HTML help text * feat(builder): add an expanded editor for head HTML * refactor(builder): reuse preview credentials for head HTML * refactor(builder): reuse custom code paths for head HTML * fix: say when head HTML is left out of Preview --------- Co-authored-by: peter_baserow <peter@baserow.io>
211 lines
5.2 KiB
Vue
211 lines
5.2 KiB
Vue
<template>
|
|
<div class="code-editor__container">
|
|
<div class="code-editor">
|
|
<EditorContent :editor="editor" />
|
|
<ButtonIcon
|
|
class="code-editor__expand"
|
|
icon="iconoir-expand"
|
|
size="small"
|
|
type="secondary"
|
|
:title="modalTitle"
|
|
:aria-label="modalTitle"
|
|
@click="showModal"
|
|
/>
|
|
</div>
|
|
<Modal
|
|
ref="modal"
|
|
class="code-editor__modal"
|
|
wide
|
|
content-scrollable
|
|
@show="focusModalEditor"
|
|
@hidden="$emit('blur')"
|
|
>
|
|
<h2 class="box__title">
|
|
{{ modalTitle }}
|
|
</h2>
|
|
<div class="code-editor code-editor--modal">
|
|
<EditorContent :editor="modalEditor" />
|
|
</div>
|
|
<p v-if="errorMessage" class="control__messages--error" role="alert">
|
|
{{ errorMessage }}
|
|
</p>
|
|
</Modal>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import { Editor, EditorContent } from '@tiptap/vue-3'
|
|
import { Placeholder } from '@tiptap/extension-placeholder'
|
|
|
|
import { Document } from '@tiptap/extension-document'
|
|
import { Text } from '@tiptap/extension-text'
|
|
import CodeAutoIndentExtension from '@baserow/modules/core/editor/codeAutoIndentExtension'
|
|
|
|
const CodeEditorDocument = Document.extend({
|
|
content: 'codeBlock',
|
|
})
|
|
|
|
export default {
|
|
name: 'CodeEditor',
|
|
components: {
|
|
EditorContent,
|
|
},
|
|
props: {
|
|
language: {
|
|
type: String,
|
|
default: 'javascript',
|
|
validator: (value) => ['javascript', 'css', 'html'].includes(value),
|
|
},
|
|
modelValue: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
placeholder: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
modalTitle: {
|
|
type: String,
|
|
default: 'Code',
|
|
},
|
|
errorMessage: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
},
|
|
emits: ['update:modelValue', 'blur'],
|
|
data() {
|
|
return {
|
|
editor: null,
|
|
modalEditor: null,
|
|
LockedCodeBlockLowlight: null,
|
|
lowlight: null,
|
|
}
|
|
},
|
|
watch: {
|
|
modelValue(newCode) {
|
|
this.setEditorContent(this.editor, newCode)
|
|
this.setEditorContent(this.modalEditor, newCode)
|
|
},
|
|
language() {
|
|
this.setEditorContent(this.editor, this.modelValue)
|
|
this.setEditorContent(this.modalEditor, this.modelValue)
|
|
},
|
|
},
|
|
async mounted() {
|
|
const [
|
|
{ CodeBlockLowlight },
|
|
{ createLowlight },
|
|
{ default: javascript },
|
|
{ default: css },
|
|
{ default: xml },
|
|
] = await Promise.all([
|
|
import('@tiptap/extension-code-block-lowlight'),
|
|
import('lowlight'),
|
|
import('highlight.js/lib/languages/javascript'),
|
|
import('highlight.js/lib/languages/css'),
|
|
import('highlight.js/lib/languages/xml'),
|
|
])
|
|
|
|
this.lowlight = createLowlight({ javascript, css, xml })
|
|
this.LockedCodeBlockLowlight = CodeBlockLowlight.extend({
|
|
addKeyboardShortcuts() {
|
|
const parentShortcuts = this.parent?.() || {}
|
|
|
|
return {
|
|
...parentShortcuts,
|
|
Backspace: () => {
|
|
const { empty, $anchor } = this.editor.state.selection
|
|
|
|
if (!empty || $anchor.parent.type.name !== this.name) {
|
|
return false
|
|
}
|
|
|
|
if ($anchor.pos === 1 || !$anchor.parent.textContent.length) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
},
|
|
}
|
|
},
|
|
})
|
|
|
|
this.editor = this.createEditor()
|
|
this.modalEditor = this.createEditor()
|
|
},
|
|
beforeUnmount() {
|
|
this.editor?.destroy()
|
|
this.modalEditor?.destroy()
|
|
},
|
|
methods: {
|
|
createEditor() {
|
|
return new Editor({
|
|
editorProps: {
|
|
attributes: {
|
|
role: 'textbox',
|
|
'aria-label': this.modalTitle,
|
|
'aria-multiline': 'true',
|
|
spellcheck: 'false',
|
|
},
|
|
},
|
|
extensions: [
|
|
CodeEditorDocument,
|
|
Text,
|
|
Placeholder.configure({
|
|
placeholder: this.placeholder,
|
|
}),
|
|
CodeAutoIndentExtension,
|
|
this.LockedCodeBlockLowlight.configure({
|
|
lowlight: this.lowlight,
|
|
exitOnTripleEnter: false,
|
|
exitOnArrowDown: false,
|
|
HTMLAttributes: {
|
|
class: 'code-editor__code-wrapper',
|
|
},
|
|
}),
|
|
],
|
|
content: this.generateCodeBlock(this.modelValue),
|
|
onUpdate: ({ editor }) => {
|
|
const currentCode = editor.getText()
|
|
if (this.modelValue !== currentCode) {
|
|
this.$emit('update:modelValue', currentCode)
|
|
}
|
|
},
|
|
onBlur: ({ event }) => this.$emit('blur', event),
|
|
})
|
|
},
|
|
generateCodeBlock(code) {
|
|
return {
|
|
type: 'doc',
|
|
content: [
|
|
{
|
|
type: 'codeBlock',
|
|
attrs: {
|
|
language: this.language,
|
|
},
|
|
content: code ? [{ type: 'text', text: code }] : [],
|
|
},
|
|
],
|
|
}
|
|
},
|
|
setEditorContent(editor, code) {
|
|
if (editor && code !== editor.getText()) {
|
|
editor.commands.setContent(this.generateCodeBlock(code), {
|
|
emitUpdate: false,
|
|
parseOptions: { preserveWhitespace: 'full' },
|
|
})
|
|
}
|
|
},
|
|
showModal() {
|
|
this.$refs.modal.show()
|
|
},
|
|
focusModalEditor() {
|
|
this.$nextTick(() => {
|
|
this.modalEditor?.commands.focus('end')
|
|
})
|
|
},
|
|
},
|
|
}
|
|
</script>
|