2020-03-10 17:43:19 +03:00
|
|
|
<template>
|
|
|
|
<div class="quill">
|
|
|
|
<div :id="toolbarId">
|
|
|
|
<div class="ql-formats">
|
|
|
|
<button class="ql-bold"></button>
|
|
|
|
<button class="ql-italic"></button>
|
|
|
|
<button class="ql-underline"></button>
|
|
|
|
<button class="ql-link"></button>
|
|
|
|
<button class="ql-blockquote"></button>
|
|
|
|
<button type="button" class="ql-list" value="ordered"></button>
|
|
|
|
<button type="button" class="ql-list" value="bullet"></button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-01-18 16:20:57 +03:00
|
|
|
|
2020-03-10 17:43:19 +03:00
|
|
|
<div :id="editorId" :name="name" class="" ref="editor">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
2021-01-18 16:20:57 +03:00
|
|
|
|
2020-03-10 17:43:19 +03:00
|
|
|
<script>
|
2021-01-18 16:20:57 +03:00
|
|
|
import Quill from 'quill';
|
|
|
|
|
2021-01-27 13:23:22 +03:00
|
|
|
//var Block = Quill.import('blots/block');
|
|
|
|
//Block.tagName = 'div';
|
|
|
|
//Quill.register(Block);
|
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
export default {
|
2020-03-10 17:43:19 +03:00
|
|
|
name: 'akaunting-html-editor',
|
2021-01-18 16:20:57 +03:00
|
|
|
|
2020-03-10 17:43:19 +03:00
|
|
|
props: {
|
2021-01-18 16:20:57 +03:00
|
|
|
name: String,
|
|
|
|
value: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
|
|
|
theme: {
|
|
|
|
type: String,
|
|
|
|
default: 'snow'
|
|
|
|
},
|
2020-03-10 17:43:19 +03:00
|
|
|
},
|
2021-01-18 16:20:57 +03:00
|
|
|
|
2020-03-10 17:43:19 +03:00
|
|
|
data () {
|
2021-01-18 16:20:57 +03:00
|
|
|
return {
|
|
|
|
editor: null,
|
2021-02-02 10:39:28 +03:00
|
|
|
editorValue: this.value,
|
2021-01-18 16:20:57 +03:00
|
|
|
content: null,
|
|
|
|
lastHtmlValue: '',
|
|
|
|
editorId: null,
|
|
|
|
toolbarId: null
|
|
|
|
}
|
2020-03-10 17:43:19 +03:00
|
|
|
},
|
2021-01-18 16:20:57 +03:00
|
|
|
|
2020-03-10 17:43:19 +03:00
|
|
|
methods: {
|
2021-01-18 16:20:57 +03:00
|
|
|
initialize (Quill) {
|
|
|
|
let theme = this.theme;
|
2020-03-10 17:43:19 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
this.editor = new Quill(`#${this.editorId}`, {
|
|
|
|
theme: theme,
|
|
|
|
modules: {
|
|
|
|
toolbar: `#${this.toolbarId}`
|
|
|
|
}
|
|
|
|
});
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-02-02 10:39:28 +03:00
|
|
|
if (this.editorValue.length > 0) {
|
|
|
|
this.editorValue = this.editorValue.replace(new RegExp('<p><br></p>', 'g'), '<p> </p>');
|
2021-01-27 13:23:22 +03:00
|
|
|
|
2021-02-02 10:39:28 +03:00
|
|
|
this.editor.pasteHTML(this.editorValue);
|
2021-01-18 16:20:57 +03:00
|
|
|
}
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
let editorRef = this.$refs.editor;
|
|
|
|
let node = editorRef.children[0];
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
this.editor.on('text-change', () => {
|
|
|
|
let html = node.innerHTML;
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
if (html === '<p><br></p>') {
|
|
|
|
html = '';
|
2021-01-27 13:23:22 +03:00
|
|
|
} else {
|
|
|
|
html = html.replace(new RegExp('<p><br></p>', 'g'), '<p> </p>');
|
2021-01-18 16:20:57 +03:00
|
|
|
}
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
this.content = html;
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
this.$emit('input', this.content);
|
|
|
|
});
|
|
|
|
},
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
pasteHTML () {
|
|
|
|
if (!this.editor) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-10 17:43:19 +03:00
|
|
|
|
2021-02-02 10:39:28 +03:00
|
|
|
this.editorValue = this.editorValue.replace(new RegExp('<p><br></p>', 'g'), '<p> </p>');
|
2021-01-27 13:23:22 +03:00
|
|
|
|
2021-02-02 10:39:28 +03:00
|
|
|
this.editor.pasteHTML(this.editorValue);
|
2021-01-18 16:20:57 +03:00
|
|
|
},
|
2020-03-10 17:43:19 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
randomString() {
|
|
|
|
let text = "";
|
|
|
|
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
|
|
}
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
return text;
|
|
|
|
},
|
|
|
|
},
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
async mounted () {
|
2021-02-02 10:39:28 +03:00
|
|
|
this.content = this.editorValue;
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
this.editorId = this.randomString();
|
|
|
|
this.toolbarId = this.randomString();
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.initialize(Quill)
|
|
|
|
});
|
2020-03-10 17:43:19 +03:00
|
|
|
},
|
2020-07-28 16:34:45 +03:00
|
|
|
|
2020-03-10 17:43:19 +03:00
|
|
|
watch: {
|
2021-01-18 16:20:57 +03:00
|
|
|
value (newVal) {
|
|
|
|
if (newVal !== this.content) {
|
|
|
|
this.pasteHTML(newVal);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-02-02 10:39:28 +03:00
|
|
|
editorValue (newVal) {
|
|
|
|
if (newVal !== this.content) {
|
|
|
|
this.pasteHTML(newVal);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-18 16:20:57 +03:00
|
|
|
content (newVal) {
|
|
|
|
this.$emit('input', newVal);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-03-10 17:43:19 +03:00
|
|
|
</script>
|