From 6b4b5efe45186acb45f9494cd0199519c3a65bb5 Mon Sep 17 00:00:00 2001 From: Hans Pagel Date: Fri, 5 Feb 2021 22:11:01 +0100 Subject: [PATCH] add savvy example --- .../Examples/MarkdownShortcuts/index.vue | 2 +- .../demos/Examples/Savvy/ColorHighlighter.ts | 35 +++++++ docs/src/demos/Examples/Savvy/findColors.ts | 33 +++++++ docs/src/demos/Examples/Savvy/index.spec.js | 7 ++ docs/src/demos/Examples/Savvy/index.vue | 92 +++++++++++++++++++ docs/src/docPages/examples/savvy.md | 3 + docs/src/links.yaml | 3 + 7 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 docs/src/demos/Examples/Savvy/ColorHighlighter.ts create mode 100644 docs/src/demos/Examples/Savvy/findColors.ts create mode 100644 docs/src/demos/Examples/Savvy/index.spec.js create mode 100644 docs/src/demos/Examples/Savvy/index.vue create mode 100644 docs/src/docPages/examples/savvy.md diff --git a/docs/src/demos/Examples/MarkdownShortcuts/index.vue b/docs/src/demos/Examples/MarkdownShortcuts/index.vue index 8f55d2ae..10055922 100644 --- a/docs/src/demos/Examples/MarkdownShortcuts/index.vue +++ b/docs/src/demos/Examples/MarkdownShortcuts/index.vue @@ -36,7 +36,7 @@ export default { You can overwrite existing input rules or add your own to nodes, marks and extensions.

- For example, we added the Typography extensions here. Try typing (c) to see how it’s converted to a proper © character. You can also try ->, >>, 1/2, !=, or --. + For example, we added the Typography extension here. Try typing (c) to see how it’s converted to a proper © character. You can also try ->, >>, 1/2, !=, or --.

`, extensions: [ diff --git a/docs/src/demos/Examples/Savvy/ColorHighlighter.ts b/docs/src/demos/Examples/Savvy/ColorHighlighter.ts new file mode 100644 index 00000000..c8a5e7a0 --- /dev/null +++ b/docs/src/demos/Examples/Savvy/ColorHighlighter.ts @@ -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, + } +} diff --git a/docs/src/demos/Examples/Savvy/findColors.ts b/docs/src/demos/Examples/Savvy/findColors.ts new file mode 100644 index 00000000..a3cd5e44 --- /dev/null +++ b/docs/src/demos/Examples/Savvy/findColors.ts @@ -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) +} diff --git a/docs/src/demos/Examples/Savvy/index.spec.js b/docs/src/demos/Examples/Savvy/index.spec.js new file mode 100644 index 00000000..6310388f --- /dev/null +++ b/docs/src/demos/Examples/Savvy/index.spec.js @@ -0,0 +1,7 @@ +context('/demos/Examples/Savvy', () => { + before(() => { + cy.visit('/demos/Examples/Savvy') + }) + + // TODO: Write tests +}) diff --git a/docs/src/demos/Examples/Savvy/index.vue b/docs/src/demos/Examples/Savvy/index.vue new file mode 100644 index 00000000..8727853a --- /dev/null +++ b/docs/src/demos/Examples/Savvy/index.vue @@ -0,0 +1,92 @@ + + + + + diff --git a/docs/src/docPages/examples/savvy.md b/docs/src/docPages/examples/savvy.md new file mode 100644 index 00000000..29a943e2 --- /dev/null +++ b/docs/src/docPages/examples/savvy.md @@ -0,0 +1,3 @@ +# Savvy + + diff --git a/docs/src/links.yaml b/docs/src/links.yaml index 5c9dfd9f..69faee28 100644 --- a/docs/src/links.yaml +++ b/docs/src/links.yaml @@ -40,6 +40,9 @@ link: /examples/minimal - title: Multiple editors link: /examples/multiple-editors + - title: Savvy + link: /examples/savvy + type: draft - title: Guide items: