UNPKG

@atlaskit/editor-plugin-synced-block

Version:

SyncedBlock plugin for @atlaskit/editor-core

60 lines (58 loc) 3.74 kB
"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 _reactIntlNext = require("react-intl-next"); 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, _reactIntlNext.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, _api$core2; api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || _api$core.actions.execute(function (_ref2) { var tr = _ref2.tr; return api === null || api === void 0 ? void 0 : api.syncedBlock.commands.insertSyncedBlock()({ tr: tr }); }); 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.createSyncBlockLabel); 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 })); };