wordpress-theme-openmamba/scripts/codemirror/addon/display/panel.js

130 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-07-28 14:57:52 +02:00
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/LICENSE
2020-10-02 13:05:50 +02:00
(function (mod) {
2019-07-28 14:57:52 +02:00
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
2020-10-02 13:05:50 +02:00
})(function (CodeMirror) {
CodeMirror.defineExtension("addPanel", function (node, options) {
2019-07-28 14:57:52 +02:00
options = options || {};
if (!this.state.panels) initPanels(this);
var info = this.state.panels;
var wrapper = info.wrapper;
var cmWrapper = this.getWrapperElement();
var replace = options.replace instanceof Panel && !options.replace.cleared;
if (options.after instanceof Panel && !options.after.cleared) {
wrapper.insertBefore(node, options.before.node.nextSibling);
} else if (options.before instanceof Panel && !options.before.cleared) {
wrapper.insertBefore(node, options.before.node);
} else if (replace) {
wrapper.insertBefore(node, options.replace.node);
2020-10-02 13:05:50 +02:00
options.replace.clear(true);
2019-07-28 14:57:52 +02:00
} else if (options.position == "bottom") {
wrapper.appendChild(node);
} else if (options.position == "before-bottom") {
wrapper.insertBefore(node, cmWrapper.nextSibling);
} else if (options.position == "after-top") {
wrapper.insertBefore(node, cmWrapper);
} else {
wrapper.insertBefore(node, wrapper.firstChild);
}
var height = (options && options.height) || node.offsetHeight;
2020-10-02 13:05:50 +02:00
var panel = new Panel(this, node, options, height);
info.panels.push(panel);
this.setSize();
2019-07-28 14:57:52 +02:00
if (options.stable && isAtTop(this, node))
2020-10-02 13:05:50 +02:00
this.scrollTo(null, this.getScrollInfo().top + height);
2019-07-28 14:57:52 +02:00
2020-10-02 13:05:50 +02:00
return panel;
2019-07-28 14:57:52 +02:00
});
function Panel(cm, node, options, height) {
this.cm = cm;
this.node = node;
this.options = options;
this.height = height;
this.cleared = false;
}
2020-10-02 13:05:50 +02:00
/* when skipRemove is true, clear() was called from addPanel().
* Thus removePanels() should not be called (issue 5518) */
Panel.prototype.clear = function (skipRemove) {
2019-07-28 14:57:52 +02:00
if (this.cleared) return;
this.cleared = true;
var info = this.cm.state.panels;
2020-10-02 13:05:50 +02:00
info.panels.splice(info.panels.indexOf(this), 1);
this.cm.setSize();
2019-07-28 14:57:52 +02:00
if (this.options.stable && isAtTop(this.cm, this.node))
this.cm.scrollTo(null, this.cm.getScrollInfo().top - this.height)
info.wrapper.removeChild(this.node);
2020-10-02 13:05:50 +02:00
if (info.panels.length == 0 && !skipRemove) removePanels(this.cm);
2019-07-28 14:57:52 +02:00
};
2020-10-02 13:05:50 +02:00
Panel.prototype.changed = function () {
this.height = this.node.getBoundingClientRect().height;
this.cm.setSize();
2019-07-28 14:57:52 +02:00
};
function initPanels(cm) {
var wrap = cm.getWrapperElement();
var style = window.getComputedStyle ? window.getComputedStyle(wrap) : wrap.currentStyle;
var height = parseInt(style.height);
var info = cm.state.panels = {
setHeight: wrap.style.height,
2020-10-02 13:05:50 +02:00
panels: [],
2019-07-28 14:57:52 +02:00
wrapper: document.createElement("div")
};
wrap.parentNode.insertBefore(info.wrapper, wrap);
var hasFocus = cm.hasFocus();
info.wrapper.appendChild(wrap);
if (hasFocus) cm.focus();
cm._setSize = cm.setSize;
2020-10-02 13:05:50 +02:00
if (height != null) cm.setSize = function (width, newHeight) {
if (!newHeight) newHeight = info.wrapper.offsetHeight;
2019-07-28 14:57:52 +02:00
info.setHeight = newHeight;
if (typeof newHeight != "number") {
var px = /^(\d+\.?\d*)px$/.exec(newHeight);
if (px) {
newHeight = Number(px[1]);
} else {
info.wrapper.style.height = newHeight;
newHeight = info.wrapper.offsetHeight;
}
}
2020-10-02 13:05:50 +02:00
var editorheight = newHeight - info.panels
.map(function (p) { return p.node.getBoundingClientRect().height; })
.reduce(function (a, b) { return a + b; }, 0);
cm._setSize(width, editorheight);
2019-07-28 14:57:52 +02:00
height = newHeight;
};
}
function removePanels(cm) {
var info = cm.state.panels;
cm.state.panels = null;
var wrap = cm.getWrapperElement();
info.wrapper.parentNode.replaceChild(wrap, info.wrapper);
wrap.style.height = info.setHeight;
cm.setSize = cm._setSize;
cm.setSize();
}
function isAtTop(cm, dom) {
for (var sibling = dom.nextSibling; sibling; sibling = sibling.nextSibling)
if (sibling == cm.getWrapperElement()) return true
return false
}
});