@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
103 lines (101 loc) • 4.64 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.datasourceDisplayInformation = exports.DatasourceDropdownOption = void 0;
var _react = _interopRequireDefault(require("react"));
var _messages = require("@atlaskit/editor-common/messages");
var _state = require("@atlaskit/editor-prosemirror/state");
var _smartLinkList = _interopRequireDefault(require("@atlaskit/icon/core/smart-link-list"));
var _linkDatasource = require("@atlaskit/link-datasource");
var _linkProvider = require("@atlaskit/link-provider");
var _menu = require("@atlaskit/menu");
var _doc = require("../pm-plugins/doc");
var _pluginKey = require("../pm-plugins/plugin-key");
var _useFetchDatasourceInfo = require("./useFetchDatasourceInfo");
var datasourceDisplayInformation = exports.datasourceDisplayInformation = {
title: _messages.cardMessages.datasourceAppearanceTitle,
icon: _smartLinkList.default,
// eslint-disable-next-line @atlaskit/editor/no-re-export
iconFallback: _smartLinkList.default
};
var DatasourceDropdownOption = exports.DatasourceDropdownOption = function DatasourceDropdownOption(_ref) {
var allowDatasource = _ref.allowDatasource,
intl = _ref.intl,
url = _ref.url,
selected = _ref.selected,
inputMethod = _ref.inputMethod,
dispatchCommand = _ref.dispatchCommand;
var cardContext = (0, _linkProvider.useSmartLinkContext)();
var _useFetchDatasourceIn = (0, _useFetchDatasourceInfo.useFetchDatasourceInfo)({
isRegularCardNode: true,
url: url,
cardContext: cardContext
}),
datasourceId = _useFetchDatasourceIn.datasourceId,
parameters = _useFetchDatasourceIn.parameters;
if (!allowDatasource || !datasourceId || !parameters) {
return null;
}
var onChangeAppearance = function onChangeAppearance(editorState, _dispatch, view) {
var _state$datasourceStas, _state$datasourceStas2;
if (!view || !datasourceId || !parameters) {
return false;
}
var state = _pluginKey.pluginKey.getState(editorState);
var newAdf = (0, _linkDatasource.buildDatasourceAdf)({
id: datasourceId,
parameters: parameters,
views: (_state$datasourceStas = state === null || state === void 0 || (_state$datasourceStas2 = state.datasourceStash[url]) === null || _state$datasourceStas2 === void 0 ? void 0 : _state$datasourceStas2.views) !== null && _state$datasourceStas !== void 0 ? _state$datasourceStas : [{
type: 'table'
}]
}, url);
var selection = editorState.selection;
var existingNode;
// Check if the selection contains a link mark
var $pos = editorState.doc.resolve(selection.from);
var isLinkMark = $pos.marks().some(function (mark) {
return mark.type === editorState.schema.marks.link;
});
// When selection is a TextNode and a link Mark is present return that node
if (selection instanceof _state.NodeSelection) {
existingNode = selection.node;
} else if (isLinkMark) {
var _editorState$doc$node;
existingNode = (_editorState$doc$node = editorState.doc.nodeAt(selection.from)) !== null && _editorState$doc$node !== void 0 ? _editorState$doc$node : undefined;
}
if (existingNode) {
(0, _doc.updateCardViaDatasource)({
state: editorState,
node: existingNode,
newAdf: newAdf,
view: view,
sourceEvent: undefined,
isDeletingConfig: true,
inputMethod: inputMethod
});
return true;
}
return false;
};
if (url) {
var _cardContext$store, _urlState$error;
var urlState = cardContext === null || cardContext === void 0 || (_cardContext$store = cardContext.store) === null || _cardContext$store === void 0 ? void 0 : _cardContext$store.getState()[url];
if ((urlState === null || urlState === void 0 || (_urlState$error = urlState.error) === null || _urlState$error === void 0 ? void 0 : _urlState$error.kind) === 'fatal') {
return null;
}
}
return /*#__PURE__*/_react.default.createElement(_menu.ButtonItem, {
key: intl.formatMessage(_messages.cardMessages.datasourceAppearanceTitle),
iconBefore: (0, _smartLinkList.default)({
label: intl.formatMessage(_messages.cardMessages.datasourceAppearanceTitle)
})
// eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
,
onClick: function onClick() {
return dispatchCommand(onChangeAppearance);
},
isSelected: selected
}, intl.formatMessage(_messages.cardMessages.datasourceAppearanceTitle));
};