@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
116 lines (112 loc) • 5.21 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.quickInsertButtonDecoration = exports.findQuickInsertInsertButtonDecoration = void 0;
var _react = require("react");
var _uuid = _interopRequireDefault(require("uuid"));
var _view = require("@atlaskit/editor-prosemirror/view");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
var _quickInsertButton = require("../ui/quick-insert-button");
var _marks = require("./utils/marks");
var _vanillaQuickInsert = require("./vanilla-quick-insert");
// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
var TYPE_QUICK_INSERT = 'INSERT_BUTTON';
var findQuickInsertInsertButtonDecoration = exports.findQuickInsertInsertButtonDecoration = function findQuickInsertInsertButtonDecoration(decorations, from, to) {
return decorations.find(from, to, function (spec) {
return spec.type === TYPE_QUICK_INSERT;
});
};
var quickInsertButtonDecoration = exports.quickInsertButtonDecoration = function quickInsertButtonDecoration(_ref) {
var api = _ref.api,
formatMessage = _ref.formatMessage,
rootPos = _ref.rootPos,
anchorName = _ref.anchorName,
nodeType = _ref.nodeType,
nodeViewPortalProviderAPI = _ref.nodeViewPortalProviderAPI,
rootAnchorName = _ref.rootAnchorName,
rootNodeType = _ref.rootNodeType,
anchorRectCache = _ref.anchorRectCache,
editorState = _ref.editorState;
// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead
var key = (0, _uuid.default)();
var cleanupCallbacks = [];
var widgetSpec = (0, _experiments.editorExperiment)('platform_editor_breakout_resizing', true) ? {
side: -2,
type: TYPE_QUICK_INSERT,
/**
* sigh - `marks` influences the position that the widget is drawn (as described on the `side` property).
* Exclude 'breakout' on purpose, so the widgets render at the top of the document to avoid z-index issues
* Other block marks must be added, otherwise PM will split the DOM elements causing mutations and re-draws
*/
marks: (0, _expValEquals.expValEquals)('platform_editor_clean_up_widget_mark_logic', 'isEnabled', true) ? [] : (0, _marks.getActiveBlockMarks)(editorState, rootPos),
destroy: function destroy(_) {
if ((0, _platformFeatureFlags.fg)('platform_editor_fix_widget_destroy')) {
nodeViewPortalProviderAPI.remove(key);
}
cleanupCallbacks.forEach(function (cb) {
cb();
});
}
} : {
side: -2,
type: TYPE_QUICK_INSERT,
destroy: function destroy(_) {
if ((0, _platformFeatureFlags.fg)('platform_editor_fix_widget_destroy')) {
nodeViewPortalProviderAPI.remove(key);
}
cleanupCallbacks.forEach(function (cb) {
cb();
});
}
};
return _view.Decoration.widget(rootPos, function (view, getPos) {
var element = document.createElement('span');
// inline decoration causes cursor disappear when focusing editor at the first line (e.g. press Tab when title is focused)
element.style.display = 'block';
// make sure it does not interfere with elements floating next to each other e.g. paragraph next to image with wrap-right
element.style.clear = 'unset';
element.contentEditable = 'false';
element.setAttribute('data-blocks-quick-insert-container', 'true');
if ((0, _platformFeatureFlags.fg)('confluence_remix_button_right_side_block_fg')) {
element.setAttribute('data-blocks-quick-insert-button', 'true');
}
element.setAttribute('data-testid', 'block-ctrl-quick-insert-button');
if ((0, _experiments.editorExperiment)('platform_editor_block_control_optimise_render', true, {
exposure: true
})) {
var vanillaElement = (0, _vanillaQuickInsert.createVanillaButton)({
formatMessage: formatMessage,
api: api,
view: view,
getPos: getPos,
cleanupCallbacks: cleanupCallbacks,
rootAnchorName: rootAnchorName !== null && rootAnchorName !== void 0 ? rootAnchorName : nodeType,
anchorName: anchorName,
rootNodeType: rootNodeType !== null && rootNodeType !== void 0 ? rootNodeType : nodeType,
anchorRectCache: anchorRectCache
});
element.appendChild(vanillaElement);
return element;
}
nodeViewPortalProviderAPI.render(function () {
return /*#__PURE__*/(0, _react.createElement)(_quickInsertButton.QuickInsertWithVisibility, {
api: api,
getPos: getPos,
formatMessage: formatMessage,
view: view,
nodeType: nodeType,
anchorName: anchorName,
rootAnchorName: rootAnchorName,
rootNodeType: rootNodeType !== null && rootNodeType !== void 0 ? rootNodeType : nodeType,
anchorRectCache: anchorRectCache
});
}, element, key, undefined,
// @portal-render-immediately
true);
return element;
}, widgetSpec);
};