mirror of
https://github.com/sparksuite/simplemde-markdown-editor.git
synced 2025-09-24 16:40:55 -06:00
simplify createToolbar
This commit is contained in:
parent
668a2fe423
commit
2a07108c23
File diff suppressed because one or more lines are too long
@ -17119,79 +17119,74 @@ var SimpleMDE = function (_Action) {
|
|||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
key: 'createToolbar',
|
key: 'createToolbar',
|
||||||
value: function createToolbar(items) {
|
value: function createToolbar() {
|
||||||
items = items || this.options.toolbar;
|
var _this4 = this;
|
||||||
|
|
||||||
|
var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.toolbar;
|
||||||
|
|
||||||
if (!items || items.length === 0) return;
|
if (!items || items.length === 0) return;
|
||||||
var i = void 0;
|
var isExistent = function isExistent(value) {
|
||||||
for (i = 0; i < items.length; i++) {
|
return _metadata.toolbarBuiltInButtons[value] != undefined;
|
||||||
if (_metadata.toolbarBuiltInButtons[items[i]] != undefined) {
|
};
|
||||||
items[i] = _metadata.toolbarBuiltInButtons[items[i]];
|
this.toolbar = items.map(function (v) {
|
||||||
}
|
return isExistent(v) ? _metadata.toolbarBuiltInButtons[v] : v;
|
||||||
}
|
});
|
||||||
|
|
||||||
var bar = document.createElement("div");
|
var bar = document.createElement("div");
|
||||||
bar.className = "editor-toolbar";
|
|
||||||
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
var toolbarData = {};
|
var toolbarData = {};
|
||||||
self.toolbar = items;
|
var nextLoop = function nextLoop(v, i) {
|
||||||
|
var name = v.name;
|
||||||
for (i = 0; i < items.length; i++) {
|
if (name == "guide" && _this4.options.toolbarGuideIcon === false) return true;
|
||||||
if (items[i].name == "guide" && self.options.toolbarGuideIcon === false) continue;
|
if (_this4.options.hideIcons && _this4.options.hideIcons.indexOf(name) != -1) return true;
|
||||||
|
|
||||||
if (self.options.hideIcons && self.options.hideIcons.indexOf(items[i].name) != -1) continue;
|
|
||||||
|
|
||||||
// Fullscreen does not work well on mobile devices (even tablets)
|
// Fullscreen does not work well on mobile devices (even tablets)
|
||||||
// In the future, hopefully this can be resolved
|
// In the future, hopefully this can be resolved
|
||||||
if ((items[i].name == "fullscreen" || items[i].name == "side-by-side") && _utils2.default.isMobile()) continue;
|
if ((name == "fullscreen" || name == "side-by-side") && _utils2.default.isMobile()) return true;
|
||||||
|
|
||||||
// Don't include trailing separators
|
// Don't include trailing separators
|
||||||
if (items[i] === "|") {
|
if (v === "|") {
|
||||||
var nonSeparatorIconsFollow = false;
|
var nonSeparatorIconsFollow = true;
|
||||||
|
for (var x = i + 1; x < _this4.toolbar.length; x++) {
|
||||||
for (var x = i + 1; x < items.length; x++) {
|
if (_this4.toolbar[x] !== "|" && (!_this4.options.hideIcons || _this4.options.hideIcons.indexOf(name) == -1)) {
|
||||||
if (items[x] !== "|" && (!self.options.hideIcons || self.options.hideIcons.indexOf(items[x].name) == -1)) {
|
nonSeparatorIconsFollow = false;
|
||||||
nonSeparatorIconsFollow = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (nonSeparatorIconsFollow) return true;
|
||||||
if (!nonSeparatorIconsFollow) continue;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
var createElement = function createElement(v) {
|
||||||
|
if (v === "|") return createSep();
|
||||||
|
return createIcon(v, _this4.options.toolbarTips, _this4.options.shortcuts);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.toolbar.every(function (v, i) {
|
||||||
|
if (nextLoop(v, i)) return false;
|
||||||
|
|
||||||
// Create the icon and append to the toolbar
|
// Create the icon and append to the toolbar
|
||||||
(function (item) {
|
var el = createElement(v);
|
||||||
var el = void 0;
|
|
||||||
if (item === "|") {
|
// bind events, special for info
|
||||||
el = createSep();
|
if (v.action) {
|
||||||
} else {
|
if (typeof v.action === "function") {
|
||||||
el = createIcon(item, self.options.toolbarTips, self.options.shortcuts);
|
el.onclick = function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
v.action(_this4);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
if (typeof v.action === "string") {
|
||||||
// bind events, special for info
|
el.href = v.action;
|
||||||
if (item.action) {
|
el.target = "_blank";
|
||||||
if (typeof item.action === "function") {
|
|
||||||
el.onclick = function (e) {
|
|
||||||
e.preventDefault();
|
|
||||||
item.action(self);
|
|
||||||
};
|
|
||||||
} else if (typeof item.action === "string") {
|
|
||||||
el.href = item.action;
|
|
||||||
el.target = "_blank";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
toolbarData[item.name || item] = el;
|
toolbarData[v.name || v] = el;
|
||||||
bar.appendChild(el);
|
bar.appendChild(el);
|
||||||
})(items[i]);
|
return true;
|
||||||
}
|
});
|
||||||
|
this.toolbarElements = toolbarData;
|
||||||
self.toolbarElements = toolbarData;
|
this.codemirror.on("cursorActivity", function () {
|
||||||
|
var stat = _base2.default.getState(_this4.codemirror);
|
||||||
var cm = this.codemirror;
|
|
||||||
cm.on("cursorActivity", function () {
|
|
||||||
var stat = _base2.default.getState(cm);
|
|
||||||
|
|
||||||
for (var key in toolbarData) {
|
for (var key in toolbarData) {
|
||||||
(function (key) {
|
(function (key) {
|
||||||
@ -17205,14 +17200,16 @@ var SimpleMDE = function (_Action) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var cmWrapper = cm.getWrapperElement();
|
var cmWrapper = this.codemirror.getWrapperElement();
|
||||||
|
|
||||||
|
bar.className = "editor-toolbar";
|
||||||
cmWrapper.parentNode.insertBefore(bar, cmWrapper);
|
cmWrapper.parentNode.insertBefore(bar, cmWrapper);
|
||||||
return bar;
|
return bar;
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
key: 'createStatusbar',
|
key: 'createStatusbar',
|
||||||
value: function createStatusbar() {
|
value: function createStatusbar() {
|
||||||
var _this4 = this;
|
var _this5 = this;
|
||||||
|
|
||||||
var status = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.status;
|
var status = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.status;
|
||||||
|
|
||||||
@ -17290,7 +17287,7 @@ var SimpleMDE = function (_Action) {
|
|||||||
if (typeof v.defaultValue === "function") v.defaultValue(el);
|
if (typeof v.defaultValue === "function") v.defaultValue(el);
|
||||||
if (typeof v.onUpdate === "function") {
|
if (typeof v.onUpdate === "function") {
|
||||||
// Create a closure around the span of the current action, then execute the onUpdate handler
|
// Create a closure around the span of the current action, then execute the onUpdate handler
|
||||||
_this4.codemirror.on("update", function () {
|
_this5.codemirror.on("update", function () {
|
||||||
return v.onUpdate(el);
|
return v.onUpdate(el);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
2
dist/simplemde.min.js
vendored
2
dist/simplemde.min.js
vendored
File diff suppressed because one or more lines are too long
@ -348,85 +348,66 @@ class SimpleMDE extends Action {
|
|||||||
return preview;
|
return preview;
|
||||||
};
|
};
|
||||||
|
|
||||||
createToolbar(items) {
|
createToolbar(items = this.options.toolbar) {
|
||||||
items = items || this.options.toolbar;
|
|
||||||
|
|
||||||
if(!items || items.length === 0) return;
|
if(!items || items.length === 0) return;
|
||||||
let i;
|
const isExistent = value => toolbarBuiltInButtons[value] != undefined
|
||||||
for(i = 0; i < items.length; i++) {
|
this.toolbar = items.map(v => isExistent(v) ? toolbarBuiltInButtons[v] : v)
|
||||||
if(toolbarBuiltInButtons[items[i]] != undefined) {
|
|
||||||
items[i] = toolbarBuiltInButtons[items[i]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let bar = document.createElement("div");
|
let bar = document.createElement("div");
|
||||||
bar.className = "editor-toolbar";
|
|
||||||
|
|
||||||
let self = this;
|
|
||||||
|
|
||||||
let toolbarData = {};
|
let toolbarData = {};
|
||||||
self.toolbar = items;
|
const nextLoop = (v, i) => {
|
||||||
|
const name = v.name
|
||||||
for(i = 0; i < items.length; i++) {
|
if(name == "guide" && this.options.toolbarGuideIcon === false) return true;
|
||||||
if(items[i].name == "guide" && self.options.toolbarGuideIcon === false)
|
if(this.options.hideIcons && this.options.hideIcons.indexOf(name) != -1) return true;
|
||||||
continue;
|
|
||||||
|
|
||||||
if(self.options.hideIcons && self.options.hideIcons.indexOf(items[i].name) != -1)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Fullscreen does not work well on mobile devices (even tablets)
|
// Fullscreen does not work well on mobile devices (even tablets)
|
||||||
// In the future, hopefully this can be resolved
|
// In the future, hopefully this can be resolved
|
||||||
if((items[i].name == "fullscreen" || items[i].name == "side-by-side") && utils.isMobile())
|
if((name == "fullscreen" || name == "side-by-side") && utils.isMobile()) return true;
|
||||||
continue;
|
|
||||||
|
|
||||||
|
|
||||||
// Don't include trailing separators
|
// Don't include trailing separators
|
||||||
if(items[i] === "|") {
|
if(v === "|") {
|
||||||
let nonSeparatorIconsFollow = false;
|
let nonSeparatorIconsFollow = true;
|
||||||
|
for(let x = (i + 1); x < this.toolbar.length; x++) {
|
||||||
for(let x = (i + 1); x < items.length; x++) {
|
if(this.toolbar[x] !== "|" && (!this.options.hideIcons || this.options.hideIcons.indexOf(name) == -1)) {
|
||||||
if(items[x] !== "|" && (!self.options.hideIcons || self.options.hideIcons.indexOf(items[x].name) == -1)) {
|
nonSeparatorIconsFollow = false;
|
||||||
nonSeparatorIconsFollow = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(nonSeparatorIconsFollow) return true
|
||||||
if(!nonSeparatorIconsFollow)
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
// Create the icon and append to the toolbar
|
const createElement = v => {
|
||||||
(function(item) {
|
if(v === "|") return createSep();
|
||||||
let el;
|
return createIcon(v, this.options.toolbarTips, this.options.shortcuts)
|
||||||
if(item === "|") {
|
|
||||||
el = createSep();
|
|
||||||
} else {
|
|
||||||
el = createIcon(item, self.options.toolbarTips, self.options.shortcuts);
|
|
||||||
}
|
|
||||||
|
|
||||||
// bind events, special for info
|
|
||||||
if(item.action) {
|
|
||||||
if(typeof item.action === "function") {
|
|
||||||
el.onclick = e => {
|
|
||||||
e.preventDefault();
|
|
||||||
item.action(self);
|
|
||||||
};
|
|
||||||
} else if(typeof item.action === "string") {
|
|
||||||
el.href = item.action;
|
|
||||||
el.target = "_blank";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
toolbarData[item.name || item] = el;
|
|
||||||
bar.appendChild(el);
|
|
||||||
})(items[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.toolbarElements = toolbarData;
|
this.toolbar.every((v, i) => {
|
||||||
|
if(nextLoop(v, i)) return false;
|
||||||
|
|
||||||
let cm = this.codemirror;
|
// Create the icon and append to the toolbar
|
||||||
cm.on("cursorActivity", () => {
|
let el = createElement(v)
|
||||||
let stat = base.getState(cm);
|
|
||||||
|
// bind events, special for info
|
||||||
|
if(v.action) {
|
||||||
|
if(typeof v.action === "function") {
|
||||||
|
el.onclick = e => {
|
||||||
|
e.preventDefault();
|
||||||
|
v.action(this);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if(typeof v.action === "string") {
|
||||||
|
el.href = v.action;
|
||||||
|
el.target = "_blank";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toolbarData[v.name || v] = el;
|
||||||
|
bar.appendChild(el);
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
this.toolbarElements = toolbarData;
|
||||||
|
this.codemirror.on("cursorActivity", () => {
|
||||||
|
let stat = base.getState(this.codemirror);
|
||||||
|
|
||||||
for(let key in toolbarData) {
|
for(let key in toolbarData) {
|
||||||
(function(key) {
|
(function(key) {
|
||||||
@ -440,7 +421,9 @@ class SimpleMDE extends Action {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const cmWrapper = cm.getWrapperElement();
|
const cmWrapper = this.codemirror.getWrapperElement();
|
||||||
|
|
||||||
|
bar.className = "editor-toolbar";
|
||||||
cmWrapper.parentNode.insertBefore(bar, cmWrapper);
|
cmWrapper.parentNode.insertBefore(bar, cmWrapper);
|
||||||
return bar;
|
return bar;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user