tiptap/examples/Components/Routes/Mentions/index.vue

144 lines
2.5 KiB
Vue
Raw Normal View History

2018-09-02 02:51:17 +08:00
<template>
<div>
2018-09-25 13:43:21 +08:00
<editor class="editor" :extensions="extensions" ref="editor">
2018-09-02 02:51:17 +08:00
<div class="editor__content" slot="content" slot-scope="props">
<h2>
Mentions
</h2>
<p>
Yeah <span data-mention-type="user" data-mention-id="1">Philipp Kühn</span> and <span data-mention-type="user" data-mention-id="2">Hans Pagel</span>.
</p>
</div>
</editor>
2018-09-25 13:43:21 +08:00
<div class="suggestions">
2018-09-25 14:37:39 +08:00
<div v-if="query && !filteredUsers.length">
No users found.
</div>
<div v-for="user in filteredUsers" :key="user.id" @click="selectUser(user)">
2018-09-25 13:43:21 +08:00
{{ user.name }}
</div>
</div>
2018-09-02 02:51:17 +08:00
</div>
</template>
<script>
2018-09-25 14:37:39 +08:00
import Fuse from 'fuse.js'
2018-09-02 02:51:17 +08:00
import Icon from 'Components/Icon'
import { Editor } from 'tiptap'
import {
BlockquoteNode,
BulletListNode,
CodeBlockNode,
HardBreakNode,
HeadingNode,
ListItemNode,
MentionNode,
OrderedListNode,
TodoItemNode,
TodoListNode,
BoldMark,
CodeMark,
ItalicMark,
LinkMark,
HistoryExtension,
} from 'tiptap-extensions'
export default {
components: {
Editor,
Icon,
},
data() {
return {
extensions: [
new BlockquoteNode(),
new BulletListNode(),
new CodeBlockNode(),
new HardBreakNode(),
new HeadingNode({ maxLevel: 3 }),
new ListItemNode(),
2018-09-06 04:09:18 +08:00
new MentionNode({
2018-09-25 13:43:21 +08:00
onEnter: args => {
console.log('start', args)
2018-09-25 14:37:39 +08:00
this.query = args.text
2018-09-06 04:09:18 +08:00
},
2018-09-25 13:43:21 +08:00
onChange: args => {
console.log('change', args)
2018-09-25 14:37:39 +08:00
this.query = args.text
2018-09-06 04:09:18 +08:00
},
2018-09-25 13:43:21 +08:00
onExit: args => {
console.log('stop', args)
2018-09-25 14:37:39 +08:00
this.query = null
2018-09-06 04:09:18 +08:00
},
}),
2018-09-02 02:51:17 +08:00
new OrderedListNode(),
new TodoItemNode(),
new TodoListNode(),
new BoldMark(),
new CodeMark(),
new ItalicMark(),
new LinkMark(),
new HistoryExtension(),
],
2018-09-25 14:37:39 +08:00
query: null,
2018-09-25 13:43:21 +08:00
users: [
{
name: 'Philipp Kühn',
id: 1,
},
{
name: 'Hans Pagel',
id: 2,
},
],
2018-09-02 02:51:17 +08:00
}
},
2018-09-25 14:37:39 +08:00
computed: {
filteredUsers() {
if (!this.query) {
return this.users
}
const fuse = new Fuse(this.users, {
threshold: 0.2,
keys: [
'name',
],
})
return fuse.search(this.query)
},
},
2018-09-25 13:43:21 +08:00
methods: {
selectUser(user) {
this.$refs.editor.menuActions.nodes.mention.command({
type: 'user',
id: user.id,
label: user.name,
})
},
},
2018-09-02 02:51:17 +08:00
}
2018-09-06 04:09:18 +08:00
</script>
<style lang="scss">
@import "~variables";
.mention {
background: rgba($color-black, 0.1);
color: rgba($color-black, 0.6);
font-size: 0.8rem;
font-weight: bold;
border-radius: 5px;
padding: 0.2rem 0.5rem;
}
2018-09-25 13:43:21 +08:00
.suggestions {
max-width: 30rem;
margin: 0 auto 2rem auto;
}
2018-09-06 04:09:18 +08:00
</style>