@wordpress/block-library
Version:
Block library for the WordPress editor.
257 lines (216 loc) • 8.58 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _i18n = require("@wordpress/i18n");
var _keycodes = require("@wordpress/keycodes");
/**
* WordPress dependencies
*/
var _window = window,
wp = _window.wp;
function isTmceEmpty(editor) {
// When tinyMce is empty the content seems to be:
// <p><br data-mce-bogus="1"></p>
// avoid expensive checks for large documents
var body = editor.getBody();
if (body.childNodes.length > 1) {
return false;
} else if (body.childNodes.length === 0) {
return true;
}
if (body.childNodes[0].childNodes.length > 1) {
return false;
}
return /^\n?$/.test(body.innerText || body.textContent);
}
var ClassicEdit =
/*#__PURE__*/
function (_Component) {
(0, _inherits2.default)(ClassicEdit, _Component);
function ClassicEdit(props) {
var _this;
(0, _classCallCheck2.default)(this, ClassicEdit);
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(ClassicEdit).call(this, props));
_this.initialize = _this.initialize.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
_this.onSetup = _this.onSetup.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
_this.focus = _this.focus.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
return _this;
}
(0, _createClass2.default)(ClassicEdit, [{
key: "componentDidMount",
value: function componentDidMount() {
var _window$wpEditorL10n$ = window.wpEditorL10n.tinymce,
baseURL = _window$wpEditorL10n$.baseURL,
suffix = _window$wpEditorL10n$.suffix;
window.tinymce.EditorManager.overrideDefaults({
base_url: baseURL,
suffix: suffix
});
if (document.readyState === 'complete') {
this.initialize();
} else {
window.addEventListener('DOMContentLoaded', this.initialize);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
window.addEventListener('DOMContentLoaded', this.initialize);
wp.oldEditor.remove("editor-".concat(this.props.clientId));
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props = this.props,
clientId = _this$props.clientId,
content = _this$props.attributes.content;
var editor = window.tinymce.get("editor-".concat(clientId));
if (prevProps.attributes.content !== content) {
editor.setContent(content || '');
}
}
}, {
key: "initialize",
value: function initialize() {
var clientId = this.props.clientId;
var settings = window.wpEditorL10n.tinymce.settings;
wp.oldEditor.initialize("editor-".concat(clientId), {
tinymce: (0, _objectSpread2.default)({}, settings, {
inline: true,
content_css: false,
fixed_toolbar_container: "#toolbar-".concat(clientId),
setup: this.onSetup
})
});
}
}, {
key: "onSetup",
value: function onSetup(editor) {
var _this2 = this;
var _this$props2 = this.props,
content = _this$props2.attributes.content,
setAttributes = _this$props2.setAttributes;
var ref = this.ref;
var bookmark;
this.editor = editor;
if (content) {
editor.on('loadContent', function () {
return editor.setContent(content);
});
}
editor.on('blur', function () {
bookmark = editor.selection.getBookmark(2, true);
setAttributes({
content: editor.getContent()
});
editor.once('focus', function () {
if (bookmark) {
editor.selection.moveToBookmark(bookmark);
}
});
return false;
});
editor.on('mousedown touchstart', function () {
bookmark = null;
});
editor.on('keydown', function (event) {
if ((event.keyCode === _keycodes.BACKSPACE || event.keyCode === _keycodes.DELETE) && isTmceEmpty(editor)) {
// delete the block
_this2.props.onReplace([]);
event.preventDefault();
event.stopImmediatePropagation();
}
var altKey = event.altKey;
/*
* Prevent Mousetrap from kicking in: TinyMCE already uses its own
* `alt+f10` shortcut to focus its toolbar.
*/
if (altKey && event.keyCode === _keycodes.F10) {
event.stopPropagation();
}
}); // TODO: the following is for back-compat with WP 4.9, not needed in WP 5.0. Remove it after the release.
editor.addButton('kitchensink', {
tooltip: (0, _i18n._x)('More', 'button to expand options'),
icon: 'dashicon dashicons-editor-kitchensink',
onClick: function onClick() {
var button = this;
var active = !button.active();
button.active(active);
editor.dom.toggleClass(ref, 'has-advanced-toolbar', active);
}
}); // Show the second, third, etc. toolbars when the `kitchensink` button is removed by a plugin.
editor.on('init', function () {
if (editor.settings.toolbar1 && editor.settings.toolbar1.indexOf('kitchensink') === -1) {
editor.dom.addClass(ref, 'has-advanced-toolbar');
}
});
editor.addButton('wp_add_media', {
tooltip: (0, _i18n.__)('Insert Media'),
icon: 'dashicon dashicons-admin-media',
cmd: 'WP_Medialib'
}); // End TODO.
editor.on('init', function () {
var rootNode = _this2.editor.getBody(); // Create the toolbar by refocussing the editor.
if (document.activeElement === rootNode) {
rootNode.blur();
_this2.editor.focus();
}
});
}
}, {
key: "focus",
value: function focus() {
if (this.editor) {
this.editor.focus();
}
}
}, {
key: "onToolbarKeyDown",
value: function onToolbarKeyDown(event) {
// Prevent WritingFlow from kicking in and allow arrows navigation on the toolbar.
event.stopPropagation(); // Prevent Mousetrap from moving focus to the top toolbar when pressing `alt+f10` on this block toolbar.
event.nativeEvent.stopImmediatePropagation();
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var clientId = this.props.clientId; // Disable reason: the toolbar itself is non-interactive, but must capture
// events from the KeyboardShortcuts component to stop their propagation.
/* eslint-disable jsx-a11y/no-static-element-interactions */
return [// Disable reason: Clicking on this visual placeholder should create
// the toolbar, it can also be created by focussing the field below.
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
(0, _element.createElement)("div", {
key: "toolbar",
id: "toolbar-".concat(clientId),
ref: function ref(_ref) {
return _this3.ref = _ref;
},
className: "block-library-classic__toolbar",
onClick: this.focus,
"data-placeholder": (0, _i18n.__)('Classic'),
onKeyDown: this.onToolbarKeyDown
}), (0, _element.createElement)("div", {
key: "editor",
id: "editor-".concat(clientId),
className: "wp-block-freeform block-library-rich-text__tinymce"
})];
/* eslint-enable jsx-a11y/no-static-element-interactions */
}
}]);
return ClassicEdit;
}(_element.Component);
exports.default = ClassicEdit;
//# sourceMappingURL=edit.js.map