现在有的接口对接测试完成
This commit is contained in:
+122
-72
@@ -1,93 +1,143 @@
|
||||
(function (window, $) {
|
||||
(function (root) {
|
||||
'use strict';
|
||||
|
||||
var defaultItems = [
|
||||
'source', '|',
|
||||
'undo', 'redo', '|',
|
||||
'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
|
||||
'plainpaste', 'wordpaste', '|',
|
||||
'justifyleft', 'justifycenter', 'justifyright', 'justifyfull',
|
||||
'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', '|',
|
||||
'formatblock', 'fontname', 'fontsize', '|',
|
||||
'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
|
||||
'strikethrough', 'lineheight', 'removeformat', '|',
|
||||
'image', 'multiimage', 'table', 'hr', 'emoticons', 'baidumap',
|
||||
'pagebreak', 'anchor', 'link', 'unlink', '|',
|
||||
'about'
|
||||
var documentRef = root.document;
|
||||
var editors = {};
|
||||
var submitBound = false;
|
||||
var toolbarKeys = [
|
||||
'headerSelect', 'fontSize', 'color', 'bgColor', '|',
|
||||
'bold', 'italic', 'underline', 'through', '|',
|
||||
'justifyLeft', 'justifyCenter', 'justifyRight', 'justifyJustify', '|',
|
||||
'insertTable', 'clearStyle', 'insertLink', 'undo', 'redo'
|
||||
];
|
||||
|
||||
function getJquery() {
|
||||
return $ || window.jQuery || (window.layui && window.layui.$);
|
||||
function findTextarea(target) {
|
||||
if (!target || !documentRef) return null;
|
||||
if (typeof target === 'string') return documentRef.querySelector(target);
|
||||
if (target.nodeType === 1) return target;
|
||||
return target[0] || null;
|
||||
}
|
||||
|
||||
function normalizeSelector(target) {
|
||||
var jq = getJquery();
|
||||
|
||||
if (!target) return '';
|
||||
if (typeof target === 'string') return target;
|
||||
if (jq && target instanceof jq) target = target.get(0);
|
||||
if (target && target.id) return '#' + target.id;
|
||||
return target;
|
||||
function ensureId(textarea) {
|
||||
if (!textarea.id) textarea.id = 'rich-editor-' + Math.floor(Math.random() * 1000000);
|
||||
return textarea.id;
|
||||
}
|
||||
|
||||
function initRichEditor(target, options) {
|
||||
var jq = getJquery();
|
||||
var selector = normalizeSelector(target);
|
||||
var $textarea;
|
||||
function fieldValue(editor) {
|
||||
return String(editor.getText() || '').trim() ? editor.getHtml() : '';
|
||||
}
|
||||
|
||||
if (!jq) return null;
|
||||
$textarea = jq(selector).first();
|
||||
if (!$textarea.length || !window.KindEditor) return null;
|
||||
function syncEditor(instance) {
|
||||
instance.textarea.value = fieldValue(instance.editor);
|
||||
}
|
||||
|
||||
if (!$textarea.attr('id')) {
|
||||
$textarea.attr('id', 'rich-editor-' + Math.floor(Math.random() * 1000000));
|
||||
selector = '#' + $textarea.attr('id');
|
||||
function createRichEditor(target, options) {
|
||||
var textarea = findTextarea(target);
|
||||
var settings = options || {};
|
||||
var id;
|
||||
var host;
|
||||
var toolbar;
|
||||
var content;
|
||||
var editor;
|
||||
var wangEditor = root.wangEditor;
|
||||
var instance;
|
||||
|
||||
if (!textarea || !wangEditor || !wangEditor.createEditor || !wangEditor.createToolbar) return null;
|
||||
if (textarea.dataset.wangEditorInitialized === 'true') return editors[textarea.id] || null;
|
||||
|
||||
id = ensureId(textarea);
|
||||
host = documentRef.createElement('div');
|
||||
host.className = 'rich-editor-host';
|
||||
toolbar = documentRef.createElement('div');
|
||||
toolbar.className = 'rich-editor-toolbar';
|
||||
toolbar.id = id + '-toolbar';
|
||||
content = documentRef.createElement('div');
|
||||
content.className = 'rich-editor-content';
|
||||
content.id = id + '-content';
|
||||
host.appendChild(toolbar);
|
||||
host.appendChild(content);
|
||||
textarea.insertAdjacentElement('afterend', host);
|
||||
|
||||
try {
|
||||
editor = wangEditor.createEditor({
|
||||
selector: '#' + content.id,
|
||||
html: textarea.value || '',
|
||||
config: Object.assign({
|
||||
placeholder: textarea.getAttribute('placeholder') || '请输入内容',
|
||||
onChange: function (currentEditor) {
|
||||
textarea.value = fieldValue(currentEditor);
|
||||
},
|
||||
onBlur: function (currentEditor) {
|
||||
textarea.value = fieldValue(currentEditor);
|
||||
}
|
||||
}, settings.editorConfig || {}),
|
||||
mode: settings.mode || 'default'
|
||||
});
|
||||
wangEditor.createToolbar({
|
||||
editor: editor,
|
||||
selector: '#' + toolbar.id,
|
||||
config: Object.assign({ toolbarKeys: toolbarKeys }, settings.toolbarConfig || {}),
|
||||
mode: settings.mode || 'default'
|
||||
});
|
||||
} catch (error) {
|
||||
host.remove();
|
||||
return null;
|
||||
}
|
||||
|
||||
return window.KindEditor.create(selector, jq.extend({
|
||||
width: '100%',
|
||||
minWidth: '100%',
|
||||
height: '460px',
|
||||
resizeType: 1,
|
||||
allowPreviewEmoticons: true,
|
||||
allowImageUpload: true,
|
||||
filterMode: false,
|
||||
newlineTag: 'p',
|
||||
items: defaultItems,
|
||||
afterBlur: function () {
|
||||
this.sync();
|
||||
}
|
||||
}, options || {}));
|
||||
textarea.hidden = true;
|
||||
textarea.setAttribute('aria-hidden', 'true');
|
||||
textarea.dataset.wangEditorInitialized = 'true';
|
||||
instance = {
|
||||
textarea: textarea,
|
||||
editor: editor,
|
||||
sync: function () { syncEditor(instance); },
|
||||
getHtml: function () { return fieldValue(editor); },
|
||||
getText: function () { return editor.getText(); }
|
||||
};
|
||||
editors[id] = instance;
|
||||
instance.sync();
|
||||
return instance;
|
||||
}
|
||||
|
||||
function initRichEditors(root, options) {
|
||||
var jq = getJquery();
|
||||
var editors = {};
|
||||
var $root;
|
||||
function initRichEditors(rootNode, options) {
|
||||
var scope = rootNode || documentRef;
|
||||
var instances = {};
|
||||
|
||||
if (!jq) return editors;
|
||||
$root = root ? jq(root) : jq(document);
|
||||
$root.find('.js-rich-editor').each(function (index) {
|
||||
var $textarea = jq(this);
|
||||
var id = $textarea.attr('id') || ('rich-editor-' + (index + 1));
|
||||
if (!scope || !scope.querySelectorAll) return instances;
|
||||
Array.prototype.forEach.call(scope.querySelectorAll('.js-rich-editor'), function (textarea) {
|
||||
var instance = createRichEditor(textarea, options);
|
||||
|
||||
$textarea.attr('id', id);
|
||||
editors[id] = initRichEditor('#' + id, options);
|
||||
if (instance) instances[textarea.id] = instance;
|
||||
});
|
||||
|
||||
return editors;
|
||||
return instances;
|
||||
}
|
||||
|
||||
window.AppRichEditor = {
|
||||
init: initRichEditor,
|
||||
initAll: initRichEditors
|
||||
function bindFormSync() {
|
||||
if (submitBound || !documentRef) return;
|
||||
submitBound = true;
|
||||
documentRef.addEventListener('submit', function (event) {
|
||||
Object.keys(editors).forEach(function (id) {
|
||||
var instance = editors[id];
|
||||
|
||||
if (instance.textarea.form === event.target) instance.sync();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function initAll() {
|
||||
bindFormSync();
|
||||
return initRichEditors(documentRef);
|
||||
}
|
||||
|
||||
root.AppRichEditor = {
|
||||
init: createRichEditor,
|
||||
initAll: initRichEditors,
|
||||
syncAll: function () {
|
||||
Object.keys(editors).forEach(function (id) { editors[id].sync(); });
|
||||
}
|
||||
};
|
||||
|
||||
if (getJquery()) {
|
||||
getJquery()(function () {
|
||||
if (getJquery()('.js-rich-editor').length) {
|
||||
initRichEditors(document);
|
||||
}
|
||||
});
|
||||
}
|
||||
})(window, window.jQuery || (window.layui && window.layui.$));
|
||||
if (!documentRef) return;
|
||||
if (documentRef.readyState === 'loading') documentRef.addEventListener('DOMContentLoaded', initAll);
|
||||
else initAll();
|
||||
})(window);
|
||||
|
||||
Reference in New Issue
Block a user