UNPKG

@atlaskit/editor-plugin-tasks-and-decisions

Version:

Tasks and decisions plugin for @atlaskit/editor-core

198 lines (197 loc) 8.52 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.tempTransformSliceToRemoveBlockTaskItem = exports.normalizeNodeForTaskTextSize = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _adfSchema = require("@atlaskit/adf-schema"); var _lists = require("@atlaskit/editor-common/lists"); var _transforms = require("@atlaskit/editor-common/transforms"); var _model = require("@atlaskit/editor-prosemirror/model"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); /** * Transforms a paste slice to handle blockTaskItem nodes when pasting into task items. * * Initially we do not support user creation of blockTaskItem - it is intended primarily * for TinyMCE migration purposes however that may change in the future. * This function handles the behavior to work around Prosemirror behaviour which decides * that blockTaskItem is the appropriate node to use here. * * @param slice - The slice being pasted * @param view - The editor view where the paste is occurring * @returns The transformed slice with blockTaskItems converted to taskItems when appropriate * * @example * ```typescript * const transformedSlice = tempTransformSliceToRemoveBlockTaskItem(pasteSlice, editorView); * ``` * * @see {@link https://hello.atlassian.net/wiki/spaces/EDITOR/pages/5626622054/Block+elements+in+task+-+Decision+log#Can-users-create-block-task-items%3F} for reasoning */ var getTaskPasteContext = function getTaskPasteContext(view) { var _schema$marks; var _view$state = view.state, schema = _view$state.schema, selection = _view$state.selection; var _schema$nodes = schema.nodes, taskItem = _schema$nodes.taskItem, blockTaskItem = _schema$nodes.blockTaskItem, paragraph = _schema$nodes.paragraph; var fontSize = (_schema$marks = schema.marks) === null || _schema$marks === void 0 ? void 0 : _schema$marks.fontSize; var $from = selection.$from; var currentParent = $from.parent; var currentNode = typeof $from.node === 'function' ? $from.node() : undefined; if ((currentParent === null || currentParent === void 0 ? void 0 : currentParent.type) === taskItem || (currentNode === null || currentNode === void 0 ? void 0 : currentNode.type) === taskItem) { return { isInTaskContext: true, smallTextAttrs: false }; } if (!blockTaskItem) { return { isInTaskContext: false, smallTextAttrs: false }; } if ((currentParent === null || currentParent === void 0 ? void 0 : currentParent.type) === blockTaskItem || (currentNode === null || currentNode === void 0 ? void 0 : currentNode.type) === blockTaskItem) { return { isInTaskContext: true, smallTextAttrs: fontSize ? (0, _lists.getFirstParagraphBlockMarkAttrs)(currentParent !== null && currentParent !== void 0 ? currentParent : currentNode, fontSize) : false }; } if ((currentParent === null || currentParent === void 0 ? void 0 : currentParent.type) === paragraph && $from.depth > 0 && $from.node($from.depth - 1).type === blockTaskItem) { return { isInTaskContext: true, smallTextAttrs: fontSize ? (0, _lists.getBlockMarkAttrs)(currentParent, fontSize) : false }; } return { isInTaskContext: false, smallTextAttrs: false }; }; var convertTaskItemToBlockTaskItem = function convertTaskItemToBlockTaskItem(node, schema, smallTextAttrs) { var fontSize = schema.marks.fontSize; return (0, _transforms.createBlockTaskItem)({ attrs: node.attrs, content: node.content, marks: [fontSize.create(smallTextAttrs)], schema: schema }); }; var addSmallTextToBlockTaskItem = function addSmallTextToBlockTaskItem(node, schema, smallTextAttrs) { var paragraph = schema.nodes.paragraph; var fontSize = schema.marks.fontSize; var newContent = []; node.content.forEach(function (child) { if (child.type === paragraph) { newContent.push(paragraph.createChecked(child.attrs, child.content, child.marks.filter(function (mark) { return mark.type !== fontSize; }).concat(fontSize.create(smallTextAttrs)))); } else { newContent.push(child); } }); return node.type.create(node.attrs, newContent); }; var normalizeBlockTaskItemToTaskItems = function normalizeBlockTaskItemToTaskItems(node, schema) { var _schema$nodes2 = schema.nodes, taskItem = _schema$nodes2.taskItem, blockTaskItem = _schema$nodes2.blockTaskItem, paragraph = _schema$nodes2.paragraph; if (!blockTaskItem || node.type !== blockTaskItem) { return [node]; } var allChildrenAreParagraphs = true; node.content.forEach(function (child) { if (child.type !== paragraph) { allChildrenAreParagraphs = false; } }); if (allChildrenAreParagraphs && node.childCount > 0) { var transformedContent = []; node.content.forEach(function (paragraphNode) { transformedContent.push(taskItem.create({ localId: _adfSchema.uuid.generate(), state: node.attrs.state || 'TODO' }, paragraphNode.content)); }); return transformedContent; } return [node]; }; var transformSliceContent = function transformSliceContent(slice, transformNode) { var transformedContent = []; slice.content.forEach(function (node) { transformedContent.push.apply(transformedContent, (0, _toConsumableArray2.default)(transformNode(node))); }); if (transformedContent.length !== slice.content.childCount || transformedContent.some(function (node, idx) { return node !== slice.content.child(idx); })) { var newFragment = _model.Fragment.from(transformedContent); return new _model.Slice(newFragment, slice.openStart, slice.openEnd); } return slice; }; var transformSliceToRemoveBlockTaskItemLegacy = function transformSliceToRemoveBlockTaskItemLegacy(slice, view) { var schema = view.state.schema; var _schema$nodes3 = schema.nodes, taskItem = _schema$nodes3.taskItem, blockTaskItem = _schema$nodes3.blockTaskItem; var isInTaskItem = view.state.selection.$from.node().type === taskItem; if (!isInTaskItem || !blockTaskItem) { return slice; } return transformSliceContent(slice, function (node) { return normalizeBlockTaskItemToTaskItems(node, schema); }); }; var _normalizeNodeForTaskTextSize = exports.normalizeNodeForTaskTextSize = function normalizeNodeForTaskTextSize(node, schema, smallTextAttrs) { var _schema$nodes4 = schema.nodes, taskList = _schema$nodes4.taskList, taskItem = _schema$nodes4.taskItem, blockTaskItem = _schema$nodes4.blockTaskItem; var fontSize = schema.marks.fontSize; if (!smallTextAttrs) { return [node]; } if ((0, _transforms.isTaskList)(node.type)) { var transformedChildren = []; node.content.forEach(function (child) { transformedChildren.push.apply(transformedChildren, (0, _toConsumableArray2.default)(_normalizeNodeForTaskTextSize(child, schema, smallTextAttrs))); }); return [taskList.create(node.attrs, transformedChildren)]; } if (blockTaskItem && fontSize) { if (node.type === taskItem) { return [convertTaskItemToBlockTaskItem(node, schema, smallTextAttrs)]; } if (node.type === blockTaskItem) { return [addSmallTextToBlockTaskItem(node, schema, smallTextAttrs)]; } } return [node]; }; var normalizeNodeForTaskPaste = function normalizeNodeForTaskPaste(node, schema, smallTextAttrs) { if (smallTextAttrs) { return _normalizeNodeForTaskTextSize(node, schema, smallTextAttrs); } return normalizeBlockTaskItemToTaskItems(node, schema); }; var tempTransformSliceToRemoveBlockTaskItem = exports.tempTransformSliceToRemoveBlockTaskItem = function tempTransformSliceToRemoveBlockTaskItem(slice, view) { var fontSizeExperimentEnabled = (0, _expValEquals.expValEquals)('platform_editor_small_font_size', 'isEnabled', true); if (!fontSizeExperimentEnabled) { return transformSliceToRemoveBlockTaskItemLegacy(slice, view); } var blockTaskItem = view.state.schema.nodes.blockTaskItem; var _getTaskPasteContext = getTaskPasteContext(view), isInTaskContext = _getTaskPasteContext.isInTaskContext, smallTextAttrs = _getTaskPasteContext.smallTextAttrs; if (!isInTaskContext || !blockTaskItem) { return slice; } return transformSliceContent(slice, function (node) { return normalizeNodeForTaskPaste(node, view.state.schema, smallTextAttrs); }); };