Tiptap Focus 扩展当用户选中默个节点获得焦点时,可以制定一个样式高亮显示让用户知道选中了当前节点。
npm install @tiptap/extension-focus
className 获得焦点时元素的样式。
Focus.configure({
className: 'focus',
})
mode 应用到节点的三种模式:全部 'all',最浅 'shallowest' 最深 'deepest'
Focus.configure({
mode: 'deepest',
})
Vue 例子
React 例子
React CSS
<template>
<editor-content :editor="editor" />
</template>
<script>
import BulletList from '@tiptap/extension-bullet-list'
import Code from '@tiptap/extension-code'
import Document from '@tiptap/extension-document'
import Focus from '@tiptap/extension-focus'
import ListItem from '@tiptap/extension-list-item'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { Editor, EditorContent } from '@tiptap/vue-3'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
}
},
mounted() {
this.editor = new Editor({
extensions: [
Document,
Paragraph,
Text,
Focus.configure({
className: 'has-focus',
mode: 'all',
}),
Code,
BulletList,
ListItem,
],
autofocus: true,
content: `
<p>
The focus extension adds a class to the focused node only. That enables you to add a custom styling to just that node. By default, it’ll add <code>.has-focus</code>, even to nested nodes.
</p>
<ul>
<li>Nested elements (like this list item) will be focused with the default setting of <code>mode: all</code>.</li>
<li>Otherwise the whole list will get the focus class, even when just a single list item is selected.</li>
</ul>
`,
})
},
beforeUnmount() {
this.editor.destroy()
},
}
</script>
<style>
.has-focus {
border-radius: 3px;
box-shadow: 0 0 0 3px #68cef8;
}
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
ul,
ol {
padding: 0 1rem;
}
blockquote {
padding-left: 1rem;
border-left: 2px solid rgba(#0D0D0D, 0.1);
}
code {
font-size: 0.9rem;
padding: 0.25em;
border-radius: 0.25em;
background-color: rgba(#616161, 0.1);
color: #616161;
box-decoration-break: clone;
}
}
</style>
import './styles.scss'
import BulletList from '@tiptap/extension-bullet-list'
import Code from '@tiptap/extension-code'
import Document from '@tiptap/extension-document'
import Focus from '@tiptap/extension-focus'
import ListItem from '@tiptap/extension-list-item'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { EditorContent, useEditor } from '@tiptap/react'
import React from 'react'
export default () => {
const editor = useEditor({
extensions: [
Document,
Paragraph,
Text,
Focus.configure({
className: 'has-focus',
mode: 'all',
}),
Code,
BulletList,
ListItem,
],
autofocus: true,
content: `
<p>
The focus extension adds a class to the focused node only. That enables you to add a custom styling to just that node. By default, it’ll add <code>.has-focus</code>, even to nested nodes.
</p>
<ul>
<li>Nested elements (like this list item) will be focused with the default setting of <code>mode: all</code>.</li>
<li>Otherwise the whole list will get the focus class, even when just a single list item is selected.</li>
</ul>
`,
})
return <EditorContent editor={editor} />
}
.has-focus {
border-radius: 3px;
box-shadow: 0 0 0 3px #68cef8;
}
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
ul,
ol {
padding: 0 1rem;
}
blockquote {
border-left: 2px solid rgba(#0d0d0d, 0.1);
padding-left: 1rem;
}
code {
background-color: rgba(#616161, 0.1);
border-radius: 0.25em;
box-decoration-break: clone;
color: #616161;
font-size: 0.9rem;
padding: 0.25em;
}
}