@atlaskit/editor-plugin-synced-block
Version:
SyncedBlock plugin for @atlaskit/editor-core
81 lines (79 loc) • 4.97 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CreateSyncedBlockButton = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _analytics = require("@atlaskit/editor-common/analytics");
var _hooks = require("@atlaskit/editor-common/hooks");
var _messages = require("@atlaskit/editor-common/messages");
var _editorPluginConnectivity = require("@atlaskit/editor-plugin-connectivity");
var _editorToolbar = require("@atlaskit/editor-toolbar");
var _blockSynced = _interopRequireDefault(require("@atlaskit/icon-lab/core/block-synced"));
var _utils = require("../pm-plugins/utils/utils");
var _types = require("../types");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var CreateSyncedBlockButton = exports.CreateSyncedBlockButton = function CreateSyncedBlockButton(_ref) {
var api = _ref.api;
var intl = (0, _reactIntl.useIntl)();
var _useSharedPluginState = (0, _hooks.useSharedPluginStateWithSelector)(api, ['selection', 'connectivity'], function (states) {
var _states$selectionStat, _states$connectivityS;
return {
selection: (_states$selectionStat = states.selectionState) === null || _states$selectionStat === void 0 ? void 0 : _states$selectionStat.selection,
mode: (_states$connectivityS = states.connectivityState) === null || _states$connectivityS === void 0 ? void 0 : _states$connectivityS.mode
};
}),
selection = _useSharedPluginState.selection,
mode = _useSharedPluginState.mode;
// for toolbar button, we allow both creating a new synced block
// and converting existing block to synced block
var canBeConverted = Boolean(selection && (0, _utils.canBeConvertedToSyncBlock)(selection));
var canInsertEmptyBlock = Boolean(selection === null || selection === void 0 ? void 0 : selection.empty);
var isDisabled = Boolean((0, _editorPluginConnectivity.isOfflineMode)(mode) || !canBeConverted && !canInsertEmptyBlock);
var onClick = (0, _react.useCallback)(function () {
var _api$core;
// Insert the synced block and stop preserving the selection in a single
// transaction so the caret that insertSyncedBlock places inside the new
// block survives (EDITOR-7949). The toolbar is not normally opened via the
// block menu, but stopping preservation is defensive and keeps behaviour
// consistent with the block-menu create item should preservation ever be
// active (e.g. a block is selected via block-controls). Then re-focus so
// the caret is active, but only when the transaction was actually
// dispatched — otherwise a failed insertion would still steal DOM focus
// into the editor (mirrors CreateSyncedBlockDropdownItem).
var dispatched = api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.execute(function (_ref2) {
var _api$blockControls;
var tr = _ref2.tr;
var result = api === null || api === void 0 ? void 0 : api.syncedBlock.commands.insertSyncedBlock(_analytics.INPUT_METHOD.SYNCED_BLOCK_TB)({
tr: tr
});
if (!result) {
return null;
}
api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 || (_api$blockControls = _api$blockControls.commands) === null || _api$blockControls === void 0 || _api$blockControls.stopPreservingSelection()({
tr: tr
});
return tr;
});
if (dispatched) {
var _api$core2;
api === null || api === void 0 || (_api$core2 = api.core) === null || _api$core2 === void 0 || _api$core2.actions.focus();
}
}, [api]);
var message = intl.formatMessage(_messages.syncBlockMessages.syncBlockLabel);
return /*#__PURE__*/_react.default.createElement(_editorToolbar.ToolbarTooltip, {
content: message
}, /*#__PURE__*/_react.default.createElement(_editorToolbar.ToolbarButton, {
label: message,
iconBefore: /*#__PURE__*/_react.default.createElement(_blockSynced.default, {
size: "small",
label: ""
}),
isDisabled: isDisabled,
testId: _types.SYNCED_BLOCK_BUTTON_TEST_ID.primaryToolbarCreate,
onClick: onClick
}));
};