mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 14:13:21 +08:00
add savvy example
This commit is contained in:
parent
820cd31e95
commit
6b4b5efe45
@ -36,7 +36,7 @@ export default {
|
||||
You can overwrite existing input rules or add your own to nodes, marks and extensions.
|
||||
</p>
|
||||
<p>
|
||||
For example, we added the <code>Typography</code> extensions here. Try typing <code>(c)</code> to see how it’s converted to a proper © character. You can also try <code>-></code>, <code>>></code>, <code>1/2</code>, <code>!=</code>, or <code>--</code>.
|
||||
For example, we added the <code>Typography</code> extension here. Try typing <code>(c)</code> to see how it’s converted to a proper © character. You can also try <code>-></code>, <code>>></code>, <code>1/2</code>, <code>!=</code>, or <code>--</code>.
|
||||
</p>
|
||||
`,
|
||||
extensions: [
|
||||
|
35
docs/src/demos/Examples/Savvy/ColorHighlighter.ts
Normal file
35
docs/src/demos/Examples/Savvy/ColorHighlighter.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { Extension } from '@tiptap/core'
|
||||
import { Plugin } from 'prosemirror-state'
|
||||
import findColors from './findColors'
|
||||
|
||||
export const ColorHighlighter = Extension.create({
|
||||
name: 'colorHighlighter',
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
return [
|
||||
new Plugin({
|
||||
state: {
|
||||
init(_, { doc }) {
|
||||
return findColors(doc)
|
||||
},
|
||||
apply(transaction, oldState) {
|
||||
return transaction.docChanged
|
||||
? findColors(transaction.doc)
|
||||
: oldState
|
||||
},
|
||||
},
|
||||
props: {
|
||||
decorations(state) {
|
||||
return this.getState(state)
|
||||
},
|
||||
},
|
||||
}),
|
||||
]
|
||||
},
|
||||
})
|
||||
|
||||
declare module '@tiptap/core' {
|
||||
interface AllExtensions {
|
||||
ColorHighlighter: typeof ColorHighlighter,
|
||||
}
|
||||
}
|
33
docs/src/demos/Examples/Savvy/findColors.ts
Normal file
33
docs/src/demos/Examples/Savvy/findColors.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { Decoration, DecorationSet } from 'prosemirror-view'
|
||||
|
||||
export default function (doc: any) {
|
||||
const hexColor = /(#[0-9a-f]{3,6})\b/ig
|
||||
const results: any[] = []
|
||||
const decorations: [any?] = []
|
||||
|
||||
doc.descendants((node: any, position: any) => {
|
||||
if (!node.isText) {
|
||||
return
|
||||
}
|
||||
|
||||
let matches
|
||||
|
||||
// eslint-disable-next-line
|
||||
while (matches = hexColor.exec(node.text)) {
|
||||
results.push({
|
||||
color: matches[0],
|
||||
from: position + matches.index,
|
||||
to: position + matches.index + matches[0].length,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
results.forEach(issue => {
|
||||
decorations.push(Decoration.inline(issue.from, issue.to, {
|
||||
class: 'color',
|
||||
style: `--color: ${issue.color}`,
|
||||
}))
|
||||
})
|
||||
|
||||
return DecorationSet.create(doc, decorations)
|
||||
}
|
7
docs/src/demos/Examples/Savvy/index.spec.js
Normal file
7
docs/src/demos/Examples/Savvy/index.spec.js
Normal file
@ -0,0 +1,7 @@
|
||||
context('/demos/Examples/Savvy', () => {
|
||||
before(() => {
|
||||
cy.visit('/demos/Examples/Savvy')
|
||||
})
|
||||
|
||||
// TODO: Write tests
|
||||
})
|
92
docs/src/demos/Examples/Savvy/index.vue
Normal file
92
docs/src/demos/Examples/Savvy/index.vue
Normal file
@ -0,0 +1,92 @@
|
||||
<template>
|
||||
<editor-content :editor="editor" />
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { Editor, EditorContent } from '@tiptap/vue-starter-kit'
|
||||
import Document from '@tiptap/extension-document'
|
||||
import Paragraph from '@tiptap/extension-paragraph'
|
||||
import Text from '@tiptap/extension-text'
|
||||
import Heading from '@tiptap/extension-heading'
|
||||
import Typography from '@tiptap/extension-typography'
|
||||
import { ColorHighlighter } from './ColorHighlighter'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
EditorContent,
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
editor: null,
|
||||
}
|
||||
},
|
||||
|
||||
mounted() {
|
||||
this.editor = new Editor({
|
||||
extensions: [
|
||||
Document,
|
||||
Paragraph,
|
||||
Text,
|
||||
Heading.configure({
|
||||
levels: [1, 2],
|
||||
}),
|
||||
Typography,
|
||||
ColorHighlighter,
|
||||
],
|
||||
content: `
|
||||
<h2>
|
||||
What’s a savvy text editor?
|
||||
</h2>
|
||||
<p>
|
||||
Maybe an editor which detects hex colors like #FFF, #0D0D0D, #616161, #A975FF, #FB5151, #FD9170, #FFCB6B, #68CEF8, #80cbc4, or #9DEF8F and adds a color swatch to them while you type.
|
||||
</p>
|
||||
<p>
|
||||
→ Or an editor, which knows »what you mean« and adds the correct characters to your text — like a “typography nerd” on your side. That should be the 1×1, right? Try it out and type (c), ->, >>, 1/2, !=, -- here:
|
||||
</p>
|
||||
<p></p>
|
||||
`,
|
||||
})
|
||||
},
|
||||
|
||||
beforeDestroy() {
|
||||
this.editor.destroy()
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
/* Basic editor styles */
|
||||
.ProseMirror {
|
||||
> * + * {
|
||||
margin-top: 0.75em;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
h4,
|
||||
h5,
|
||||
h6 {
|
||||
line-height: 1.1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Color swatches */
|
||||
.color {
|
||||
white-space: nowrap;
|
||||
|
||||
&::before {
|
||||
content: ' ';
|
||||
display: inline-block;
|
||||
width: 1em;
|
||||
height: 1em;
|
||||
border: 1px solid rgba(128, 128, 128, 0.3);
|
||||
vertical-align: middle;
|
||||
margin-right: 0.1em;
|
||||
margin-bottom: 0.15em;
|
||||
border-radius: 2px;
|
||||
background-color: var(--color);
|
||||
}
|
||||
}
|
||||
</style>
|
3
docs/src/docPages/examples/savvy.md
Normal file
3
docs/src/docPages/examples/savvy.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Savvy
|
||||
|
||||
<demo name="Examples/Savvy" />
|
@ -40,6 +40,9 @@
|
||||
link: /examples/minimal
|
||||
- title: Multiple editors
|
||||
link: /examples/multiple-editors
|
||||
- title: Savvy
|
||||
link: /examples/savvy
|
||||
type: draft
|
||||
|
||||
- title: Guide
|
||||
items:
|
||||
|
Loading…
Reference in New Issue
Block a user