UNPKG

@atlaskit/adf-schema

Version:

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

112 lines (110 loc) 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMediaSpec = void 0; var _nodeTypes = require("../../next-schema/generated/nodeTypes"); var _colors = require("../../utils/colors"); var _uuid = require("../../utils/uuid"); var _camelCaseToKebabCase = require("./camel-case-to-kebab-case"); var _copyPrivateAttributes = require("./copy-private-attributes"); var PRIVATE_ATTR_PREFIX_REGEX = /^__/; var createMediaSpec = exports.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 = (0, _camelCaseToKebabCase.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.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(_colors.N30, "; box-shadow: 0 1px 1px rgba(9, 30, 66, 0.2), 0 0 1px 0 rgba(9, 30, 66, 0.24);") }; (0, _copyPrivateAttributes.copyPrivateAttributes)(node.attrs, attrs, attributes, function (key) { return "data-".concat((0, _camelCaseToKebabCase.camelCaseToKebabCase)(key.slice(2))); }); return ["".concat(domNodeType), attrs]; }; if (inline) { return (0, _nodeTypes.mediaInline)({ parseDOM: parseDOM, toDOM: toDOM }); } return (0, _nodeTypes.media)({ 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.uuid.generate(); } return attrs; } }]), toDOM: toDOM }); };