UNPKG

@atlaskit/adf-schema

Version:

Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs

106 lines (105 loc) 4.25 kB
import { media as mediaFactory, mediaInline as mediaInlineFactory } from '../../next-schema/generated/nodeTypes'; import { N30 } from '../../utils/colors'; import { uuid } from '../../utils/uuid'; import { camelCaseToKebabCase } from './camel-case-to-kebab-case'; import { copyPrivateAttributes } from './copy-private-attributes'; var PRIVATE_ATTR_PREFIX_REGEX = /^__/; export var createMediaSpec = function createMediaSpec(attributes) { var inline = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var generateLocalId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var domNodeType = inline ? 'span' : 'div'; var nodeName = inline ? 'mediaInline' : 'media'; var parseDOM = [{ tag: "".concat(domNodeType, "[data-node-type=\"").concat(nodeName, "\"]"), getAttrs: function getAttrs(dom) { var attrs = {}; if (attributes) { Object.keys(attributes).forEach(function (k) { // eslint-disable-next-line @atlassian/perf-linting/no-expensive-split-replace -- Ignored via go/ees017 (to be fixed) var key = camelCaseToKebabCase(k).replace(PRIVATE_ATTR_PREFIX_REGEX, ''); var value = // eslint-disable-next-line @atlaskit/editor/no-as-casting dom.getAttribute("data-".concat(key)) || ''; if (value) { attrs[k] = value; } }); } // Need to do validation & type conversion manually if (attrs.__fileSize) { attrs.__fileSize = +attrs.__fileSize; } var width = Number(attrs.width); if (typeof width !== 'undefined' && !isNaN(width)) { attrs.width = width; } var height = Number(attrs.height); if (typeof height !== 'undefined' && !isNaN(height)) { attrs.height = height; } if (generateLocalId) { attrs.localId = uuid.generate(); } return attrs; } }, // Don't match data URI { tag: 'img[src^="data:image"]', ignore: true }]; var toDOM = function toDOM(node) { var attrs = { 'data-id': node.attrs.id, 'data-node-type': "".concat(nodeName), 'data-type': node.attrs.type, 'data-collection': node.attrs.collection, 'data-occurrence-key': node.attrs.occurrenceKey, 'data-width': node.attrs.width, 'data-height': node.attrs.height, 'data-url': node.attrs.url, 'data-alt': node.attrs.alt, 'data-local-id': node.attrs.localId || undefined, // toDOM is used for static rendering as well as editor rendering. This comes into play for // emails, copy/paste, etc, so the title and styling here *is* useful (despite a React-based // node view being used for editing). title: 'Attachment', // Manually kept in sync with the style of media cards. The goal is to render a plain gray // rectangle that provides an affordance for media. style: "display: inline-block; border-radius: 3px; background: ".concat(N30, "; box-shadow: 0 1px 1px rgba(9, 30, 66, 0.2), 0 0 1px 0 rgba(9, 30, 66, 0.24);") }; copyPrivateAttributes(node.attrs, attrs, attributes, function (key) { return "data-".concat(camelCaseToKebabCase(key.slice(2))); }); return ["".concat(domNodeType), attrs]; }; if (inline) { return mediaInlineFactory({ parseDOM: parseDOM, toDOM: toDOM }); } return mediaFactory({ parseDOM: [].concat(parseDOM, [{ // media-inline.ts uses this same function to generate the nodespec // this ensures that we don't make a media inline out of a copied image // https://product-fabric.atlassian.net/browse/EDM-2996 tag: 'img:not(.smart-link-icon)', getAttrs: function getAttrs(dom) { // eslint-disable-next-line @typescript-eslint/no-explicit-any var attrs = { type: 'external', // eslint-disable-next-line @atlaskit/editor/no-as-casting url: dom.getAttribute('src') || '', // eslint-disable-next-line @atlaskit/editor/no-as-casting alt: dom.getAttribute('alt') || '' }; if (generateLocalId) { attrs.localId = uuid.generate(); } return attrs; } }]), toDOM: toDOM }); };