@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
96 lines (95 loc) • 4.15 kB
JavaScript
import React from 'react';
import { cardMessages as messages } from '@atlaskit/editor-common/messages';
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import SmartLinkListIcon from '@atlaskit/icon/core/smart-link-list';
import { buildDatasourceAdf } from '@atlaskit/link-datasource';
import { useSmartLinkContext } from '@atlaskit/link-provider';
import { ButtonItem } from '@atlaskit/menu';
import { updateCardViaDatasource } from '../pm-plugins/doc';
import { pluginKey } from '../pm-plugins/plugin-key';
import { useFetchDatasourceInfo } from './useFetchDatasourceInfo';
export var datasourceDisplayInformation = {
title: messages.datasourceAppearanceTitle,
icon: SmartLinkListIcon,
// eslint-disable-next-line @atlaskit/editor/no-re-export
iconFallback: SmartLinkListIcon
};
export var 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 = useSmartLinkContext();
var _useFetchDatasourceIn = 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.getState(editorState);
var newAdf = 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 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) {
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.createElement(ButtonItem, {
key: intl.formatMessage(messages.datasourceAppearanceTitle),
iconBefore: SmartLinkListIcon({
label: intl.formatMessage(messages.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.datasourceAppearanceTitle));
};