2021-01-18 05:50:19 +00:00
|
|
|
<template>
|
2021-01-26 18:34:11 +00:00
|
|
|
<div class="editor-root">
|
|
|
|
<tag-agents
|
|
|
|
v-if="showUserMentions && isPrivate"
|
|
|
|
:search-key="mentionSearchKey"
|
|
|
|
@click="insertMentionNode"
|
|
|
|
/>
|
|
|
|
<div ref="editor"></div>
|
|
|
|
</div>
|
2021-01-18 05:50:19 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import { EditorView } from 'prosemirror-view';
|
2021-01-26 18:34:11 +00:00
|
|
|
import { defaultMarkdownSerializer } from 'prosemirror-markdown';
|
2021-01-18 05:50:19 +00:00
|
|
|
|
|
|
|
const TYPING_INDICATOR_IDLE_TIME = 4000;
|
|
|
|
|
2021-01-26 18:34:11 +00:00
|
|
|
import {
|
|
|
|
addMentionsToMarkdownSerializer,
|
|
|
|
addMentionsToMarkdownParser,
|
|
|
|
schemaWithMentions,
|
|
|
|
} from '@chatwoot/prosemirror-schema/src/mentions/schema';
|
|
|
|
import {
|
|
|
|
suggestionsPlugin,
|
|
|
|
triggerCharacters,
|
|
|
|
} from '@chatwoot/prosemirror-schema/src/mentions/plugin';
|
|
|
|
import TagAgents from '../conversation/TagAgents.vue';
|
|
|
|
import { EditorState } from 'prosemirror-state';
|
|
|
|
import { defaultMarkdownParser } from 'prosemirror-markdown';
|
|
|
|
import { wootWriterSetup } from '@chatwoot/prosemirror-schema';
|
|
|
|
|
|
|
|
const createState = (content, placeholder, plugins = []) => {
|
|
|
|
return EditorState.create({
|
|
|
|
doc: addMentionsToMarkdownParser(defaultMarkdownParser).parse(content),
|
|
|
|
plugins: wootWriterSetup({
|
|
|
|
schema: schemaWithMentions,
|
|
|
|
placeholder,
|
|
|
|
plugins,
|
|
|
|
}),
|
2021-01-18 05:50:19 +00:00
|
|
|
});
|
2021-01-26 18:34:11 +00:00
|
|
|
};
|
2021-01-18 05:50:19 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'WootMessageEditor',
|
2021-01-26 18:34:11 +00:00
|
|
|
components: { TagAgents },
|
2021-01-18 05:50:19 +00:00
|
|
|
props: {
|
|
|
|
value: { type: String, default: '' },
|
|
|
|
placeholder: { type: String, default: '' },
|
2021-01-26 18:34:11 +00:00
|
|
|
isPrivate: { type: Boolean, default: false },
|
2021-01-18 05:50:19 +00:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
lastValue: null,
|
2021-01-26 18:34:11 +00:00
|
|
|
showUserMentions: false,
|
|
|
|
mentionSearchKey: '',
|
|
|
|
editorView: null,
|
|
|
|
range: null,
|
2021-01-18 05:50:19 +00:00
|
|
|
};
|
|
|
|
},
|
2021-01-26 18:34:11 +00:00
|
|
|
computed: {
|
|
|
|
plugins() {
|
|
|
|
return [
|
|
|
|
suggestionsPlugin({
|
|
|
|
matcher: triggerCharacters('@'),
|
|
|
|
onEnter: args => {
|
|
|
|
this.showUserMentions = true;
|
|
|
|
this.range = args.range;
|
|
|
|
this.editorView = args.view;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
onChange: args => {
|
|
|
|
this.editorView = args.view;
|
|
|
|
this.range = args.range;
|
|
|
|
|
|
|
|
this.mentionSearchKey = args.text.replace('@', '');
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
onExit: () => {
|
|
|
|
this.mentionSearchKey = '';
|
|
|
|
this.showUserMentions = false;
|
|
|
|
this.editorView = null;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
onKeyDown: ({ event }) => {
|
|
|
|
return event.keyCode === 13 && this.showUserMentions;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
},
|
2021-01-18 05:50:19 +00:00
|
|
|
watch: {
|
2021-01-26 18:34:11 +00:00
|
|
|
showUserMentions(updatedValue) {
|
|
|
|
this.$emit('toggle-user-mention', this.isPrivate && updatedValue);
|
|
|
|
},
|
2021-01-18 05:50:19 +00:00
|
|
|
value(newValue) {
|
|
|
|
if (newValue !== this.lastValue) {
|
2021-01-26 18:34:11 +00:00
|
|
|
this.state = createState(newValue, this.placeholder, this.plugins);
|
2021-01-18 05:50:19 +00:00
|
|
|
this.view.updateState(this.state);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
created() {
|
2021-01-26 18:34:11 +00:00
|
|
|
this.state = createState(this.value, this.placeholder, this.plugins);
|
2021-01-18 05:50:19 +00:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.view = new EditorView(this.$refs.editor, {
|
|
|
|
state: this.state,
|
|
|
|
dispatchTransaction: tx => {
|
|
|
|
this.state = this.state.apply(tx);
|
2021-01-26 18:34:11 +00:00
|
|
|
this.emitOnChange();
|
2021-01-18 05:50:19 +00:00
|
|
|
},
|
|
|
|
handleDOMEvents: {
|
|
|
|
keyup: () => {
|
|
|
|
this.onKeyup();
|
|
|
|
},
|
|
|
|
focus: () => {
|
|
|
|
this.onFocus();
|
|
|
|
},
|
|
|
|
blur: () => {
|
|
|
|
this.onBlur();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
methods: {
|
2021-01-26 18:34:11 +00:00
|
|
|
insertMentionNode(mentionItem) {
|
|
|
|
if (!this.view) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const node = this.view.state.schema.nodes.mention.create({
|
|
|
|
userId: mentionItem.key,
|
|
|
|
userFullName: mentionItem.label,
|
|
|
|
});
|
|
|
|
|
|
|
|
const tr = this.view.state.tr.replaceWith(
|
|
|
|
this.range.from,
|
|
|
|
this.range.to,
|
|
|
|
node
|
|
|
|
);
|
|
|
|
this.state = this.view.state.apply(tr);
|
|
|
|
return this.emitOnChange();
|
|
|
|
},
|
|
|
|
emitOnChange() {
|
|
|
|
this.view.updateState(this.state);
|
|
|
|
this.lastValue = addMentionsToMarkdownSerializer(
|
|
|
|
defaultMarkdownSerializer
|
|
|
|
).serialize(this.state.doc);
|
|
|
|
this.$emit('input', this.lastValue);
|
|
|
|
},
|
|
|
|
hideMentions() {
|
|
|
|
this.showUserMentions = false;
|
|
|
|
},
|
2021-01-18 05:50:19 +00:00
|
|
|
resetTyping() {
|
|
|
|
this.$emit('typing-off');
|
|
|
|
this.idleTimer = null;
|
|
|
|
},
|
|
|
|
turnOffIdleTimer() {
|
|
|
|
if (this.idleTimer) {
|
|
|
|
clearTimeout(this.idleTimer);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onKeyup() {
|
|
|
|
if (!this.idleTimer) {
|
|
|
|
this.$emit('typing-on');
|
|
|
|
}
|
|
|
|
this.turnOffIdleTimer();
|
|
|
|
this.idleTimer = setTimeout(
|
|
|
|
() => this.resetTyping(),
|
|
|
|
TYPING_INDICATOR_IDLE_TIME
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onBlur() {
|
|
|
|
this.turnOffIdleTimer();
|
|
|
|
this.resetTyping();
|
|
|
|
this.$emit('blur');
|
|
|
|
},
|
|
|
|
onFocus() {
|
|
|
|
this.$emit('focus');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.ProseMirror-menubar-wrapper {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
|
|
> .ProseMirror {
|
|
|
|
padding: 0;
|
2021-04-01 15:39:27 +00:00
|
|
|
word-break: break-word;
|
2021-01-18 05:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.editor-root {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.ProseMirror-woot-style {
|
|
|
|
min-height: 8rem;
|
|
|
|
max-height: 12rem;
|
|
|
|
overflow: auto;
|
|
|
|
}
|
2021-01-26 18:34:11 +00:00
|
|
|
|
|
|
|
.is-private {
|
|
|
|
.prosemirror-mention-node {
|
|
|
|
font-weight: var(--font-weight-medium);
|
|
|
|
background: var(--s-300);
|
|
|
|
border-radius: var(--border-radius-small);
|
|
|
|
padding: 1px 4px;
|
|
|
|
color: var(--white);
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 05:50:19 +00:00
|
|
|
</style>
|