add tippy

This commit is contained in:
Philipp Kühn 2021-01-18 12:40:13 +01:00
parent 06b88977fc
commit f96dff1f0f
5 changed files with 87 additions and 22 deletions

View File

@ -25,6 +25,7 @@
"remark-toc": "^7.0.0", "remark-toc": "^7.0.0",
"remixicon": "^2.5.0", "remixicon": "^2.5.0",
"simplify-js": "^1.2.4", "simplify-js": "^1.2.4",
"tippy.js": "^6.2.7",
"vue-github-button": "^1.1.2", "vue-github-button": "^1.1.2",
"vue-live": "^1.16.0", "vue-live": "^1.16.0",
"y-indexeddb": "^9.0.6", "y-indexeddb": "^9.0.6",

View File

@ -5,6 +5,8 @@
</template> </template>
<script> <script>
import Vue from 'vue'
import tippy, { sticky } from 'tippy.js'
import { Editor } from '@tiptap/core' import { Editor } from '@tiptap/core'
import { EditorContent } from '@tiptap/vue' import { EditorContent } from '@tiptap/vue'
import Document from '@tiptap/extension-document' import Document from '@tiptap/extension-document'
@ -29,7 +31,53 @@ export default {
Document, Document,
Paragraph, Paragraph,
Text, Text,
Mention, Mention.configure({
items: query => {
console.log('items', query)
return [query]
},
renderer: () => {
let popup
const Component = new (Vue.extend({
template: '<div>YAAAAY</div>',
}))().$mount()
return {
onStart(props) {
console.log('start')
popup = tippy('.app', {
getReferenceClientRect: () => props.virtualNode.getBoundingClientRect(),
appendTo: () => document.body,
interactive: true,
sticky: true, // make sure position of tippy is updated when content changes
plugins: [sticky],
content: Component.$el,
trigger: 'mouseenter', // manual
showOnCreate: true,
theme: 'dark',
placement: 'top-start',
inertia: true,
duration: [400, 200],
})
},
onUpdate(props) {
console.log('update', props)
},
onExit(props) {
console.log('exit', props)
if (popup) {
popup[0].destroy()
}
Component.$destroy()
},
}
},
}),
], ],
content: ` content: `
<p>text <span data-mention></span></p> <p>text <span data-mention></span></p>

View File

@ -1,15 +1,14 @@
import { Node } from '@tiptap/core' import { Node } from '@tiptap/core'
import Suggestion from '@tiptap/suggestion' import Suggestion, { SuggestionOptions } from '@tiptap/suggestion'
export interface MentionOptions { export type MentionOptions = Omit<SuggestionOptions, 'editor'>
renderer: any,
}
export const Mention = Node.create({ export const Mention = Node.create({
name: 'mention', name: 'mention',
defaultOptions: <MentionOptions>{ defaultOptions: <MentionOptions>{
renderer: null, char: '@',
renderer: () => ({}),
}, },
group: 'inline', group: 'inline',
@ -47,8 +46,7 @@ export const Mention = Node.create({
return [ return [
Suggestion({ Suggestion({
editor: this.editor, editor: this.editor,
char: '@', ...this.options,
renderer: this.options.renderer,
}), }),
] ]
}, },

View File

@ -12,11 +12,12 @@ export interface SuggestionOptions {
suggestionClass?: string, suggestionClass?: string,
command?: () => any, command?: () => any,
items?: (query: string) => any[], items?: (query: string) => any[],
onStart?: (props: any) => any, renderer?: () => {
onUpdate?: (props: any) => any, onStart?: (props: any) => any,
onExit?: (props: any) => any, onUpdate?: (props: any) => any,
onKeyDown?: (props: any) => any, onExit?: (props: any) => any,
renderer?: any, onKeyDown?: (props: any) => any,
},
} }
export function Suggestion({ export function Suggestion({
@ -27,13 +28,14 @@ export function Suggestion({
suggestionClass = 'suggestion', suggestionClass = 'suggestion',
command = () => null, command = () => null,
items = () => [], items = () => [],
onStart = () => null, // onStart = () => null,
onUpdate = () => null, // onUpdate = () => null,
onExit = () => null, // onExit = () => null,
onKeyDown = () => null, // onKeyDown = () => null,
renderer = () => ({}), renderer = () => ({}),
}: SuggestionOptions) { }: SuggestionOptions) {
// const testRenderer = renderer()
const testRenderer = renderer?.()
return new Plugin({ return new Plugin({
key: new PluginKey('suggestions'), key: new PluginKey('suggestions'),
@ -92,15 +94,18 @@ export function Suggestion({
// Trigger the hooks when necessary // Trigger the hooks when necessary
if (handleExit) { if (handleExit) {
onExit(props) // onExit(props)
testRenderer?.onExit?.(props)
} }
if (handleChange) { if (handleChange) {
onUpdate(props) // onUpdate(props)
testRenderer?.onUpdate?.(props)
} }
if (handleStart) { if (handleStart) {
onStart(props) // onStart(props)
testRenderer?.onStart?.(props)
} }
}, },
} }
@ -173,7 +178,8 @@ export function Suggestion({
return false return false
} }
return onKeyDown({ view, event, range }) // return onKeyDown({ view, event, range })
return testRenderer?.onKeyDown?.({ view, event, range })
}, },
// Setup decorator on the currently active suggestion. // Setup decorator on the currently active suggestion.

View File

@ -2231,6 +2231,11 @@
"@octokit/openapi-types" "^1.2.0" "@octokit/openapi-types" "^1.2.0"
"@types/node" ">= 8" "@types/node" ">= 8"
"@popperjs/core@^2.4.4":
version "2.6.0"
resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.6.0.tgz#f022195afdfc942e088ee2101285a1d31c7d727f"
integrity sha512-cPqjjzuFWNK3BSKLm0abspP0sp/IGOli4p5I5fKFAzdS8fvjdOwDCfZqAaIiXd9lPkOWi3SUUfZof3hEb7J/uw==
"@rollup/plugin-babel@^5.2.1": "@rollup/plugin-babel@^5.2.1":
version "5.2.2" version "5.2.2"
resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.2.2.tgz#e5623a01dd8e37e004ba87f2de218c611727d9b2" resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.2.2.tgz#e5623a01dd8e37e004ba87f2de218c611727d9b2"
@ -14120,6 +14125,13 @@ tiny-emitter@^2.0.0:
resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.1.0.tgz#1d1a56edfc51c43e863cbb5382a72330e3555423" resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.1.0.tgz#1d1a56edfc51c43e863cbb5382a72330e3555423"
integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q== integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==
tippy.js@^6.2.7:
version "6.2.7"
resolved "https://registry.yarnpkg.com/tippy.js/-/tippy.js-6.2.7.tgz#62fb34eda23f7d78151ddca922b62818c1ab9869"
integrity sha512-k+kWF9AJz5xLQHBi3K/XlmJiyu+p9gsCyc5qZhxxGaJWIW8SMjw1R+C7saUnP33IM8gUhDA2xX//ejRSwqR0tA==
dependencies:
"@popperjs/core" "^2.4.4"
tmp@^0.0.33: tmp@^0.0.33:
version "0.0.33" version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9" resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"