murph-skeleton/assets/js/admin/modules/editor.js

158 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-06-09 16:25:08 +02:00
const $ = require('jquery')
const Vue = require('vue').default
const FileManager = require('../components/file-manager/FileManager').default
2021-06-22 15:17:56 +02:00
const createModal = function () {
let container = $('#fm-modal')
const body = $('body')
if (!container.length) {
2021-06-22 15:17:56 +02:00
container = $('<div id="fm-modal" class="modal">')
body.append(container)
}
container.html(`
<div class="modal-dialog modal-dialog-large">
<div class="modal-content">
<div class="modal-body">
2021-06-22 15:17:56 +02:00
<div id="fm-modal-content">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
`)
$(container).modal('show')
return $(container)
}
const fileManagerBrowser = function (callback) {
const container = createModal()
2021-06-22 15:17:56 +02:00
const clickCallback = (e) => {
callback($(e.target).attr('data-value'), {})
$('#modal-container').modal('hide')
container.modal('hide')
2021-06-22 15:17:56 +02:00
$('body').off('click', '#file-manager-insert', clickCallback)
}
$('body').on('click', '#file-manager-insert', clickCallback)
2021-06-23 09:39:51 +02:00
return new Vue({
2021-06-22 15:17:56 +02:00
el: '#fm-modal-content',
template: '<FileManager context="tinymce" />',
components: {
FileManager
}
})
}
2021-06-09 16:25:08 +02:00
2021-06-23 09:39:51 +02:00
if (typeof window.tinymce !== 'undefined') {
window.tinymce.murph = window.tinymce.murph || {}
window.tinymce.murph.selector = window.tinymce.murph.selector || '*[data-tinymce]'
window.tinymce.murph.configurationBase = window.tinymce.murph.configurationBase || {
2021-06-15 14:16:07 +02:00
base_url: '/vendor/tinymce/',
cache_suffix: '?v=4.1.6',
importcss_append: true,
image_caption: true,
noneditable_noneditable_class: 'mceNonEditable',
toolbar_drawer: 'sliding',
spellchecker_dialog: true,
tinycomments_mode: 'embedded',
convert_urls: false,
file_picker_callback: fileManagerBrowser,
file_picker_types: 'image',
2021-06-15 14:16:07 +02:00
init_instance_callback: function (editor) {
editor.on('SetContent', () => {
2021-06-23 09:39:51 +02:00
window.tinymce.triggerSave(false, true)
2021-06-15 14:16:07 +02:00
})
editor.on('Change', () => {
2021-06-23 09:39:51 +02:00
window.tinymce.triggerSave(false, true)
2021-06-15 14:16:07 +02:00
})
2021-06-09 16:25:08 +02:00
}
2021-06-15 14:16:07 +02:00
}
2021-06-23 09:39:51 +02:00
window.tinymce.murph.modes = window.tinymce.murph.modes || {}
2021-06-15 14:16:07 +02:00
2021-06-23 09:39:51 +02:00
window.tinymce.murph.modes.default = window.tinymce.murph.modes.default || {
2021-06-15 14:16:07 +02:00
plugins: 'print preview importcss searchreplace visualblocks visualchars fullscreen template table charmap hr pagebreak nonbreaking toc insertdatetime advlist lists wordcount textpattern noneditable help charmap quickbars link image code autoresize',
menubar: 'file edit view insert format tools table tc help',
toolbar: 'undo redo | bold italic underline strikethrough | link image | fontselect fontsizeselect formatselect | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist checklist | forecolor backcolor casechange permanentpen formatpainter removeformat | pagebreak | charmap | fullscreen preview',
2021-06-15 14:16:07 +02:00
quickbars_selection_toolbar: 'bold italic | quicklink h2 h3 blockquote quickimage quicktable',
contextmenu: 'link image imagetools table configurepermanentpen'
}
2021-06-23 09:39:51 +02:00
window.tinymce.murph.modes.light = window.tinymce.murph.modes.light || {
2021-06-15 14:16:07 +02:00
contextmenu: 'link image imagetools table configurepermanentpen',
quickbars_selection_toolbar: 'bold italic',
toolbar: 'undo redo | bold italic underline'
}
2021-06-09 16:25:08 +02:00
}
const buildConfiguration = (conf) => {
2021-06-23 09:39:51 +02:00
return Object.assign({}, window.tinymce.murph.configurationBase, conf)
2021-06-09 16:25:08 +02:00
}
const makeId = () => {
2021-06-15 14:16:07 +02:00
let result = ''
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length
2021-06-09 16:25:08 +02:00
2021-06-15 14:16:07 +02:00
for (let i = 0; i < 20; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength))
}
2021-06-09 16:25:08 +02:00
2021-06-15 14:16:07 +02:00
return 'tinymce-' + result
2021-06-09 16:25:08 +02:00
}
const doInitEditor = () => {
2021-06-23 09:39:51 +02:00
$(window.tinymce.murph.selector).each((i, v) => {
2021-06-15 14:16:07 +02:00
const element = $(v)
let id = null
if (element.attr('id')) {
id = element.attr('id')
} else {
id = makeId()
element.attr('id', makeId)
}
2021-06-09 16:25:08 +02:00
2021-06-15 14:16:07 +02:00
let mode = element.attr('data-tinymce')
2021-06-09 16:25:08 +02:00
2021-06-15 14:16:07 +02:00
if (!mode) {
mode = 'default'
}
2021-06-09 16:25:08 +02:00
2021-06-23 09:39:51 +02:00
if (!Object.prototype.hasOwnProperty.call(window.tinymce.murph.modes, mode)) {
2021-06-15 14:16:07 +02:00
return
}
2021-06-09 16:25:08 +02:00
2021-06-23 09:39:51 +02:00
const conf = buildConfiguration(window.tinymce.murph.modes[mode])
2021-06-15 14:16:07 +02:00
conf.mode = 'exact'
conf.elements = id
2021-06-09 16:25:08 +02:00
2021-06-23 09:39:51 +02:00
window.tinymce.init(conf)
2021-06-15 14:16:07 +02:00
})
}
2021-06-15 14:16:07 +02:00
module.exports = function () {
if (typeof tinymce === 'undefined') {
return
}
2021-06-15 14:16:07 +02:00
const observer = new MutationObserver(doInitEditor)
const config = { attributes: false, childList: true, subtree: true }
observer.observe(document.querySelector('body'), config)
2021-06-03 13:51:43 +02:00
$(() => {
doInitEditor()
})
2021-06-15 14:16:07 +02:00
}