UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

145 lines (139 loc) 7.92 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.titleUrlPairFromNode = exports.selectedCardAppearance = exports.mergeCardInfo = exports.isEmbedSupportedAtPosition = exports.isDatasourceNode = exports.isDatasourceConfigEditable = exports.isDatasourceAdfAttributes = exports.isBlockSupportedAtPosition = exports.getResolvedAttributesFromStore = exports.getAwarenessProps = exports.focusEditorView = exports.findCardInfo = exports.displayInfoForCard = exports.appearanceForNodeType = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _model = require("@atlaskit/editor-prosemirror/model"); var _state = require("@atlaskit/editor-prosemirror/state"); var _resolvedAttributes = require("@atlaskit/link-analytics/resolved-attributes"); var _linkDatasource = require("@atlaskit/link-datasource"); var _pluginKey = require("./plugin-key"); var appearanceForNodeType = exports.appearanceForNodeType = function appearanceForNodeType(spec) { if (spec.name === 'inlineCard') { return 'inline'; } else if (spec.name === 'blockCard') { return 'block'; } else if (spec.name === 'embedCard') { return 'embed'; } return; }; var selectedCardAppearance = exports.selectedCardAppearance = function selectedCardAppearance(state) { if (state.selection instanceof _state.NodeSelection) { return appearanceForNodeType(state.selection.node.type); } }; var titleUrlPairFromNode = exports.titleUrlPairFromNode = function titleUrlPairFromNode(node) { var attrs = node.attrs; return { url: attrs.url || attrs.data && attrs.data.url, title: attrs.data && attrs.data.title }; }; /** * Merges the title and url from attributes and CardInfo from the resolved view, preferring the CardInfo. * @param titleUrlPair title and url information from the node attributes * @param info information stored in state from the resolved UI component view */ var mergeCardInfo = exports.mergeCardInfo = function mergeCardInfo(titleUrlPair, info) { return { title: info && info.title || titleUrlPair.title, url: info && info.url || titleUrlPair.url }; }; var displayInfoForCard = exports.displayInfoForCard = function displayInfoForCard(node, info) { return mergeCardInfo(titleUrlPairFromNode(node), info); }; var findCardInfo = exports.findCardInfo = function findCardInfo(state) { var pluginState = _pluginKey.pluginKey.getState(state); if (!pluginState) { return undefined; } return pluginState.cards.find(function (cardInfo) { return cardInfo.pos === state.selection.from; }); }; var isAppearanceSupportedInParent = function isAppearanceSupportedInParent(currentNodePosition, editorState, fragment, currentAppearance) { var resolvedPosition = editorState.doc.resolve(currentNodePosition); var parent = currentAppearance === 'embed' || currentAppearance === 'block' ? resolvedPosition.node() : resolvedPosition.node(-1); return parent && parent.type.validContent(fragment); }; var isEmbedSupportedAtPosition = exports.isEmbedSupportedAtPosition = function isEmbedSupportedAtPosition(currentNodePosition, editorState, currentAppearance) { return isAppearanceSupportedInParent(currentNodePosition, editorState, _model.Fragment.from(editorState.schema.nodes.embedCard.createChecked({})), currentAppearance); }; var isBlockSupportedAtPosition = exports.isBlockSupportedAtPosition = function isBlockSupportedAtPosition(currentNodePosition, editorState, currentAppearance) { return isAppearanceSupportedInParent(currentNodePosition, editorState, _model.Fragment.from(editorState.schema.nodes.blockCard.createChecked({})), currentAppearance); }; var getResolvedAttributesFromStore = exports.getResolvedAttributesFromStore = function getResolvedAttributesFromStore(url, display, store) { if (!store) { return {}; } var urlState = store === null || store === void 0 ? void 0 : store.getState()[url]; var displayCategory = display === 'url' ? 'link' : undefined; return (0, _resolvedAttributes.getResolvedAttributes)({ url: url, displayCategory: displayCategory }, urlState === null || urlState === void 0 ? void 0 : urlState.details); }; var isDatasourceConfigEditable = exports.isDatasourceConfigEditable = function isDatasourceConfigEditable(datasourceId) { var datasourcesWithConfigModal = [_linkDatasource.JIRA_LIST_OF_LINKS_DATASOURCE_ID, _linkDatasource.ASSETS_LIST_OF_LINKS_DATASOURCE_ID, _linkDatasource.CONFLUENCE_SEARCH_DATASOURCE_ID]; return datasourcesWithConfigModal.includes(datasourceId); }; /** * Typeguard that checks node attributes are datasource node attributes * ** WARNING ** Typeguards are not a guarantee, if the asserted type changes * this function will not be updated automatically */ var isDatasourceAdfAttributes = exports.isDatasourceAdfAttributes = function isDatasourceAdfAttributes(attrs) { // Check is attributes object if (!((0, _typeof2.default)(attrs) === 'object' && attrs !== null)) { return false; } // Check datasource attribute is an object if (!('datasource' in attrs)) { return false; } if ((0, _typeof2.default)(attrs.datasource) !== 'object' || attrs.datasource === null) { return false; } var hasId = 'id' in attrs.datasource && typeof attrs.datasource.id === 'string'; var hasParameters = 'parameters' in attrs.datasource && (0, _typeof2.default)(attrs.datasource.parameters) === 'object' && attrs.datasource.parameters !== null && !Array.isArray(attrs.datasource.parameters); var hasViews = 'views' in attrs.datasource && Array.isArray(attrs.datasource.views); return hasId && hasParameters && hasViews; }; /** * Typeguard that checks a node is a datasource node (blockCard and has datasource attributes) * ** WARNING ** Typeguards are not a guarantee, if the asserted type changes * this function will not be updated automatically */ var isDatasourceNode = exports.isDatasourceNode = function isDatasourceNode(node) { if (!node) { return false; } return node.type.name === 'blockCard' && isDatasourceAdfAttributes(node.attrs); }; /** * Focuses the editorView if it's not already focused. * @param editorView The editor view to focus. */ var focusEditorView = exports.focusEditorView = function focusEditorView(editorView) { if (!editorView.hasFocus()) { editorView.focus(); } }; var getAwarenessProps = exports.getAwarenessProps = function getAwarenessProps(editorState, getPos, allowEmbeds, allowBlockCards) { var _editorState$selectio, _editorState$selectio2; var disableOverlay = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false; var getPosFunction = typeof getPos !== 'boolean' ? getPos : undefined; var linkPosition = getPosFunction === null || getPosFunction === void 0 ? void 0 : getPosFunction(); var canBeUpgradedToEmbed = !!linkPosition && allowEmbeds ? isEmbedSupportedAtPosition(linkPosition, editorState, 'inline') : false; var canBeUpgradedToBlock = !!linkPosition && allowBlockCards ? isBlockSupportedAtPosition(linkPosition, editorState, 'inline') : false; var isSelected = editorState.selection instanceof _state.NodeSelection && ((_editorState$selectio = editorState.selection) === null || _editorState$selectio === void 0 || (_editorState$selectio = _editorState$selectio.node) === null || _editorState$selectio === void 0 ? void 0 : _editorState$selectio.type) === editorState.schema.nodes.inlineCard && ((_editorState$selectio2 = editorState.selection) === null || _editorState$selectio2 === void 0 ? void 0 : _editorState$selectio2.from) === (getPosFunction === null || getPosFunction === void 0 ? void 0 : getPosFunction()); return { isPulseEnabled: canBeUpgradedToEmbed, isOverlayEnabled: !disableOverlay && (canBeUpgradedToEmbed || canBeUpgradedToBlock), isSelected: isSelected }; };