UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

105 lines 5.31 kB
var _this = this; import * as tslib_1 from "tslib"; import { AddMarkStep, ReplaceStep, } from '../../prosemirror'; import { endPositionOfParent } from '../../utils'; import { posOfMediaGroupBelow, posOfParentMediaGroup } from './utils'; import { uuid } from '../utils'; import { unsupportedNodeTypesForMediaCards } from '../../schema/unsupported'; import analyticsService from '../../analytics/service'; export var insertLinks = function (view, stateManager, handleMediaState, linkRanges, linkCreateContext, collection) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var state, $to, trQueue; return tslib_1.__generator(this, function (_a) { if (!linkRanges || linkRanges.length <= 0 || !collection) { return [2 /*return*/]; } state = view.state; $to = state.selection.$to; if (unsupportedNodeTypesForMediaCards.has($to.parent.type.name)) { analyticsService.trackEvent('atlassian.editor.media.file.unsupported.node'); return [2 /*return*/]; } trQueue = new Array(); return [2 /*return*/, Promise.all(linkRanges.map(function (_a) { var href = _a.href, pos = _a.pos; return new Promise(function (resolve) { var state = view.state, dispatch = view.dispatch; var posAtTheEndOfDoc = state.doc.nodeSize - 4; var tr = state.tr; var id = "temporary:" + uuid() + ":" + href; var node = state.schema.nodes.media.create({ id: id, type: 'link', collection: collection }); stateManager.subscribe(id, handleMediaState); // If there's multiple replace steps, make sure subsequent transactions are mapped onto new positions trQueue.forEach(function (tr) { return pos = tr.mapping.map(pos); }); var $latestPos = tr.doc.resolve(pos > posAtTheEndOfDoc ? posAtTheEndOfDoc : pos); var insertPos = posOfMediaGroupBelow(state, $latestPos, false) || posOfParentMediaGroup(state, $latestPos, false) || endPositionOfParent($latestPos); // Insert an empty paragraph in case we've reached the end of the document if (insertPos === state.doc.nodeSize - 2) { tr.insert(insertPos, state.schema.nodes.paragraph.create()); } tr.replaceWith(insertPos, insertPos, node); trQueue.push(tr); dispatch(tr); analyticsService.trackEvent('atlassian.editor.media.link'); var updateStateWithError = function (error) { return stateManager.updateState(id, { id: id, status: 'error', error: error, }) || resolve(); }; var isAppWithoutURL = function (metadata) { return metadata && metadata.resources && metadata.resources.app && !metadata.resources.app.url; }; // Unfurl URL using media API linkCreateContext.getUrlPreviewProvider(href).observable().subscribe(function (metadata) { // Workaround for problem with missing fields preventing Twitter links from working if (isAppWithoutURL(metadata)) { metadata.resources.app.url = metadata.url; } linkCreateContext.addLinkItem(href, collection, metadata) .then(function (publicId) { return stateManager.updateState(id, { id: id, publicId: publicId, status: 'ready' }) || resolve(publicId); }) .catch(updateStateWithError); }, updateStateWithError); }); }))]; }); }); }; export var detectLinkRangesInSteps = function (tr, link, offset) { return tr.steps.reduce(function (linkRanges, step) { var rangeWithUrls; if (step instanceof AddMarkStep) { rangeWithUrls = findRangesWithUrlsInAddMarkStep(step, link); } else if (step instanceof ReplaceStep) { rangeWithUrls = findRangesWithUrlsInReplaceStep(step, link, offset); } return linkRanges.concat(rangeWithUrls || []); }, []); }; var findRangesWithUrlsInAddMarkStep = function (step, link) { var mark = step.mark; if (link.isInSet([mark]) && mark.attrs.href) { return [{ href: mark.attrs.href, pos: step.from, }]; } }; var findRangesWithUrlsInReplaceStep = function (step, link, offset) { var urls = new Array(); step.slice.content.descendants(function (child, pos, parent) { var linkMark = link.isInSet(child.marks); if (linkMark && linkMark.attrs.href) { urls.push({ href: linkMark.attrs.href, pos: pos + offset, }); } }); return urls; }; //# sourceMappingURL=media-links.js.map