123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <template>
- <div class="com-editor ui-input"></div>
- </template>
- <script>
- import config from "../../config";
- // import { htmlCut } from "../../utils/string";
- export default {
- props: {
- placeholder: String,
- maxlength: Number,
- html: String
- },
- mounted() {
- this.quill = new Quill(this.$el, {
- modules: {
- toolbar: false
- },
- placeholder: this.placeholder
- //preserveWhitespace:true
- });
- this.quill.root.innerHTML = this.html
- // this.$nextTick(() => (this.quill.root.innerHTML = html));
- const $editable = $(this.$el).find("[contenteditable]"); //this.$el.querySelector("[contenteditable]");
- $editable.on("paste", e => {
- e.preventDefault();
- let clp = (e.originalEvent || e).clipboardData;
- let text;
- if (clp === undefined || clp === null) {
- text = window.clipboardData.getData("text") || "";
- } else {
- text = clp.getData("text/plain") || "";
- }
- if (text) {
- //this.quill.root.innerHTML = this.quill.root.innerHTML+text;
- let index = this.quill.selection.savedRange.index
- if (typeof index == "undefined") {
- index = this.quill.getLength() - 1;
- }
- this.quill.insertText(index, text);
- setTimeout(() => {
- this.quill.setSelection(index + text.length);
- }, 10);
- }
- });
- $editable.on("mousewheel touchstart touchmove", e => {
- if (this.quill.hasFocus()) {
- e.stopPropagation();
- }
- });
- // this.quill.on("selection-change", (range, oldRange, source) => {
- // this._focusin = range !== null;
- // });
- // this.quill.on("text-change", (delta, oldDelta, source) => {
- // let size = this.quill.getLength() - 1;
- // let html = this.quill.root.innerHTML;
- // if (this.maxlength > 0 && size > this.maxlength) {
- // html = this.quill.root.innerHTML = htmlCut(
- // html,
- // this.maxlength
- // );
- // size = this.quill.getLength() - 1;
- // }
- // this.$emit("change", { html, size });
- // });
- this.quill.on("text-change", (delta, old, source) => {
- let html = this.quill.root.innerHTML == '<p><br></p>' ? '' : this.quill.root.innerHTML;
- // 过滤emoji表情
- html = html.replace(/(\ud83c[\udf00-\udfff])|(\ud83d[\udc00-\ude4f])|(\ud83d[\ude80-\udeff])/g, function (char) {
- var H, L, code;
- if (char.length === 2) {
- return ""
- } else {
- return char;
- }
- });
- if (this.maxlength > 0 && this.quill.getLength() > this.maxlength) {
- setTimeout(() => {
- this.quill.deleteText(
- this.maxlength,
- this.quill.getLength()
- );
- let size = this.quill.getLength() - 1;
- this.$emit("change", { html, size });
- }, 300);
- } else {
- let size = this.quill.getLength() - 1;
- this.$emit("change", { html, size });
- }
- });
- },
- methods: {
- setHtml(html = "") {
- this.$nextTick(() => (this.quill.root.innerHTML = html));
- //this.quill.clipboard.dangerouslyPasteHTML(0, html);
- },
- addLink(text, link) {
- if (!text || !link) {
- return;
- }
- if (!/http(s)?:\/\//.test(link)) {
- link = "https://" + link;
- }
- // if (!this._focusin) {
- // this.quill.focus();
- // }
- if (!this.quill.hasFocus()) {
- this.quill.focus();
- }
- let index = (this.quill.getSelection() || {}).index;
- if (typeof index == "undefined") {
- index = this.quill.getLength() - 1;
- }
- this.quill.insertText(index, text, "link", link);
- this.quill.blur();
- }
- }
- };
- </script>
- <style lang="less">
- .com-editor {
- height: 100%;
- padding: 0 0 30px 0;
- .ql-editor {
- padding: 10px;
- user-select: auto;
- font-size: 14px;
- white-space: normal !important;
- a {
- color: @color;
- }
- }
- .ql-editor.ql-blank::before {
- left: 10px;
- right: 10px;
- top: 10px;
- color: #505050;
- }
- .ql-clipboard {
- left: -100000px;
- height: 1px;
- overflow-y: hidden;
- position: absolute;
- top: 50%;
- }
- }
- [show-mode="mobile"],
- [edit-mode="mobile"] {
- .com-editor {
- .ql-editor {
- font-size: 100%;
- }
- }
- }
- </style>
|