2022-05-10 19:28:09 +08:00
|
|
|
<template>
|
2022-08-04 17:06:25 +08:00
|
|
|
<div class="tinymce-wrapper">
|
2022-07-11 16:44:27 +08:00
|
|
|
<div class="tinymce-container" :class="{ 'error': error }">
|
|
|
|
<form class="tiny-mce-editor" role="form" :action="updateUrl" accept-charset="UTF-8" data-remote="true" method="post">
|
|
|
|
<input type="hidden" name="_method" value="patch">
|
2022-09-05 17:29:19 +08:00
|
|
|
<div class="hidden tinymce-cancel-button tox-mbtn" tabindex="-1">
|
2022-12-09 19:52:18 +08:00
|
|
|
<button type="button" tabindex="-1">
|
|
|
|
<span class="fas fa-times"></span>
|
|
|
|
<span class="mce-txt">{{ i18n.t('general.cancel') }}</span>
|
|
|
|
</button>
|
2022-07-11 16:44:27 +08:00
|
|
|
</div>
|
2022-09-05 17:29:19 +08:00
|
|
|
<div class="hidden tinymce-save-button tox-mbtn" tabindex="-1">
|
2022-12-09 19:52:18 +08:00
|
|
|
<button type="button" tabindex="-1" >
|
2022-07-11 16:44:27 +08:00
|
|
|
<span class="fas fa-check"></span>
|
|
|
|
<span class="mce-txt">{{ i18n.t('general.save') }}</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="hidden tinymce-status-badge pull-right">
|
|
|
|
<i class="fas fa-check-circle"></i>
|
|
|
|
<span>{{ i18n.t('tiny_mce.saved_label') }}</span>
|
|
|
|
</div>
|
2022-12-09 19:52:18 +08:00
|
|
|
|
2022-07-11 16:44:27 +08:00
|
|
|
<div :id="`${objectType}_view_${objectId}`"
|
|
|
|
@click="initTinymce"
|
|
|
|
v-html="value_html"
|
|
|
|
class="ql-editor tinymce-view"
|
|
|
|
:data-placeholder="placeholder"
|
|
|
|
:data-tinymce-init="`tinymce-${objectType}-description-${objectId}`">
|
|
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
|
|
<textarea :id="`${objectType}_textarea_${objectId}`"
|
|
|
|
class="form-control hidden"
|
|
|
|
autocomplete="off"
|
|
|
|
:data-tinymce-object="`tinymce-${objectType}-description-${objectId}`"
|
|
|
|
:data-object-type="objectType"
|
|
|
|
:data-object-id="objectId"
|
|
|
|
:data-last-updated="lastUpdated * 1000"
|
|
|
|
:data-tinymce-asset-path="this.getStaticUrl('tiny-mce-assets-url')"
|
2022-08-10 16:27:15 +08:00
|
|
|
:placeholder="placeholder"
|
2022-07-11 16:44:27 +08:00
|
|
|
:value="value"
|
|
|
|
cols="120"
|
|
|
|
rows="10"
|
|
|
|
:name="fieldName"
|
|
|
|
aria-hidden="true">
|
|
|
|
</textarea>
|
2022-12-22 17:37:47 +08:00
|
|
|
<input type="hidden" class="tiny-mce-images" name="tiny_mce_images" value="[]">
|
2022-07-11 16:44:27 +08:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
2022-08-04 17:06:25 +08:00
|
|
|
<div v-if="active && error" class="tinymce-error">
|
2022-07-11 16:44:27 +08:00
|
|
|
{{ error }}
|
|
|
|
</div>
|
2022-05-10 19:28:09 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-03-30 19:27:10 +08:00
|
|
|
import UtilsMixin from '../mixins/utils.js';
|
2022-08-04 17:06:25 +08:00
|
|
|
|
2022-05-10 19:28:09 +08:00
|
|
|
export default {
|
|
|
|
name: 'Tinymce',
|
|
|
|
props: {
|
|
|
|
value: String,
|
|
|
|
value_html: String,
|
|
|
|
placeholder: String,
|
|
|
|
updateUrl: String,
|
|
|
|
objectType: String,
|
|
|
|
objectId: Number,
|
|
|
|
fieldName: String,
|
|
|
|
lastUpdated: Number,
|
2022-07-11 16:44:27 +08:00
|
|
|
inEditMode: Boolean,
|
|
|
|
characterLimit: {
|
|
|
|
type: Number,
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
characterCount: 0,
|
2022-08-04 17:06:25 +08:00
|
|
|
blurEventHandler: null,
|
|
|
|
active: false
|
2022-07-11 16:44:27 +08:00
|
|
|
}
|
2022-05-10 19:28:09 +08:00
|
|
|
},
|
2022-08-04 17:06:25 +08:00
|
|
|
mixins: [ UtilsMixin ],
|
2022-05-10 19:28:09 +08:00
|
|
|
watch: {
|
|
|
|
inEditMode() {
|
|
|
|
if (this.inEditMode) {
|
2022-08-04 17:06:25 +08:00
|
|
|
this.initTinymce();
|
2022-12-09 19:52:18 +08:00
|
|
|
} else {
|
|
|
|
this.wrapTables();
|
2022-05-10 19:28:09 +08:00
|
|
|
}
|
2023-02-28 23:22:35 +08:00
|
|
|
|
|
|
|
this.initCodeHighlight();
|
2022-07-11 16:44:27 +08:00
|
|
|
},
|
|
|
|
characterCount() {
|
2022-09-02 17:19:54 +08:00
|
|
|
if (this.editorInstance()) {
|
|
|
|
this.editorInstance().blurDisabled = this.error != false ;
|
2022-07-11 16:44:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
error() {
|
|
|
|
if(this.characterLimit && this.characterCount > this.characterLimit) {
|
|
|
|
return(
|
2023-04-05 21:34:48 +08:00
|
|
|
this.i18n.t('errors.general_text_too_long')
|
|
|
|
);
|
2022-07-11 16:44:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2022-05-10 19:28:09 +08:00
|
|
|
}
|
|
|
|
},
|
2022-07-14 00:02:35 +08:00
|
|
|
mounted() {
|
|
|
|
if (this.inEditMode) {
|
|
|
|
this.initTinymce();
|
2022-12-09 19:52:18 +08:00
|
|
|
} else {
|
|
|
|
this.wrapTables();
|
2022-07-14 00:02:35 +08:00
|
|
|
}
|
2023-02-28 23:22:35 +08:00
|
|
|
|
|
|
|
this.initCodeHighlight();
|
2022-07-14 00:02:35 +08:00
|
|
|
},
|
2022-05-10 19:28:09 +08:00
|
|
|
methods: {
|
2022-08-05 17:23:36 +08:00
|
|
|
initTinymce(e) {
|
2022-05-10 19:28:09 +08:00
|
|
|
let textArea = `#${this.objectType}_textarea_${this.objectId}`;
|
2022-08-05 17:23:36 +08:00
|
|
|
|
2022-09-02 17:19:54 +08:00
|
|
|
if (this.active) return
|
2022-09-20 21:44:52 +08:00
|
|
|
if (e && $(e.target).prop("tagName") === 'A') return
|
2022-08-05 17:23:36 +08:00
|
|
|
if (e && $(e.target).hasClass('atwho-user-popover')) return
|
|
|
|
if (e && $(e.target).hasClass('record-info-link')) return
|
|
|
|
if (e && $(e.target).parent().hasClass('atwho-inserted')) return
|
|
|
|
|
2022-12-09 19:52:18 +08:00
|
|
|
TinyMCE.init(textArea, {
|
|
|
|
onSaveCallback: (data) => {
|
|
|
|
if (data.data) {
|
|
|
|
this.$emit('update', data.data)
|
2022-09-05 17:29:19 +08:00
|
|
|
}
|
2022-12-09 19:52:18 +08:00
|
|
|
this.$emit('editingDisabled');
|
2023-01-19 19:56:15 +08:00
|
|
|
this.wrapTables();
|
2023-02-28 23:22:35 +08:00
|
|
|
this.initCodeHighlight();
|
2022-12-09 19:52:18 +08:00
|
|
|
},
|
|
|
|
afterInitCallback: () => {
|
|
|
|
this.active = true;
|
|
|
|
this.initCharacterCount();
|
|
|
|
this.$emit('editingEnabled');
|
|
|
|
},
|
|
|
|
placeholder: this.placeholder
|
2022-05-10 19:28:09 +08:00
|
|
|
}
|
2022-12-09 19:52:18 +08:00
|
|
|
)
|
2022-05-10 19:28:09 +08:00
|
|
|
},
|
|
|
|
getStaticUrl(name) {
|
|
|
|
return $(`meta[name=\'${name}\']`).attr('content');
|
2022-07-11 16:44:27 +08:00
|
|
|
},
|
2022-12-09 19:52:18 +08:00
|
|
|
wrapTables() {
|
2022-12-14 21:32:23 +08:00
|
|
|
this.$nextTick(() => {
|
2023-01-19 19:56:15 +08:00
|
|
|
TinyMCE.wrapTables($(this.$el).find('.tinymce-view'));
|
2022-12-14 21:32:23 +08:00
|
|
|
});
|
2022-12-09 19:52:18 +08:00
|
|
|
},
|
2022-07-11 16:44:27 +08:00
|
|
|
initCharacterCount() {
|
2022-09-02 17:19:54 +08:00
|
|
|
if (!this.editorInstance()) return;
|
2022-08-04 17:06:25 +08:00
|
|
|
|
2022-12-09 19:52:18 +08:00
|
|
|
this.characterCount = this.editorInstance().plugins.wordcount.body.getCharacterCount();
|
2022-09-02 17:19:54 +08:00
|
|
|
this.editorInstance().on('input change paste keydown', (e) => {
|
2022-12-09 19:52:18 +08:00
|
|
|
e.currentTarget && (this.characterCount = this.editorInstance().plugins.wordcount.body.getCharacterCount());
|
2022-07-11 16:44:27 +08:00
|
|
|
});
|
|
|
|
|
2022-09-02 17:19:54 +08:00
|
|
|
this.editorInstance().on('remove', () => this.active = false);
|
2022-08-04 17:06:25 +08:00
|
|
|
|
2022-07-11 16:44:27 +08:00
|
|
|
// clear error on cancel
|
2022-09-02 17:19:54 +08:00
|
|
|
$(this.editorInstance().container).find('.tinymce-cancel-button').on('click', ()=> {
|
2022-07-11 16:44:27 +08:00
|
|
|
this.characterCount = 0;
|
|
|
|
});
|
2022-09-02 17:19:54 +08:00
|
|
|
},
|
|
|
|
editorInstance() {
|
2022-12-09 19:52:18 +08:00
|
|
|
return tinyMCE.activeEditor;
|
2023-02-28 23:22:35 +08:00
|
|
|
},
|
|
|
|
initCodeHighlight() {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
Prism.highlightAllUnder(this.$el);
|
|
|
|
});
|
2022-05-10 19:28:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-11 17:18:40 +08:00
|
|
|
</script>
|