@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
148 lines (136 loc) • 6.31 kB
JavaScript
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { mapSlice } from '../utils/slice';
export const defaultWordWrapState = false;
// Remove the wrap WeakMap fallback when cleaning up platform_editor_code_block_q4_lovability
export const codeBlockWrappedStates = new WeakMap();
export const getDefaultCodeBlockAttrs = attrs => {
if (!expValEquals('platform_editor_code_block_q4_lovability', 'isEnabled', true)) {
return attrs;
}
// Only boolean wrap values represent caller intent. null/undefined means unset.
if ((attrs === null || attrs === void 0 ? void 0 : attrs.wrap) === true || (attrs === null || attrs === void 0 ? void 0 : attrs.wrap) === false) {
return attrs;
}
return {
...attrs,
wrap: true
};
};
export const defaultWrapForMarkdownCodeBlocksInSlice = (slice, schema) => {
if (!expValEquals('platform_editor_code_block_q4_lovability', 'isEnabled', true)) {
return slice;
}
return mapSlice(slice, node => {
if (node.type !== schema.nodes.codeBlock || node.attrs.wrap === true) {
return node;
}
// Markdown conversion uses MarkdownParser token mappings and creates code block nodes
// with the schema-default wrap:false. Since Markdown has no wrap syntax, treat that
// default as missing user intent and change it to wrap:true.
return node.type.create({
...node.attrs,
wrap: true
}, node.content, node.marks);
});
};
// Code folding state management - similar to word wrapping
const codeBlockFoldStates = new WeakMap();
export const isCodeBlockWordWrapEnabled = codeBlockNode => {
if (expValEquals('platform_editor_code_block_q4_lovability', 'isEnabled', true)) {
return Boolean(codeBlockNode.attrs.wrap);
}
const currentNodeWordWrapState = codeBlockWrappedStates.get(codeBlockNode);
return currentNodeWordWrapState !== undefined ? currentNodeWordWrapState : defaultWordWrapState;
};
export const areCodeBlockLineNumbersHidden = codeBlockNode => {
if (!expValEquals('platform_editor_code_block_q4_lovability', 'isEnabled', true)) {
return false;
}
return Boolean(codeBlockNode.attrs.hideLineNumbers);
};
export const areCodeBlockLineNumbersVisible = codeBlockNode => !areCodeBlockLineNumbersHidden(codeBlockNode);
export const getCodeBlockFoldState = codeBlockNode => {
const currentNodeFoldState = codeBlockFoldStates.get(codeBlockNode);
return currentNodeFoldState || [];
};
export const setCodeBlockFoldState = (codeBlockNode, foldRanges) => {
codeBlockFoldStates.set(codeBlockNode, foldRanges);
};
/**
* Swap the old node key with the new node key in the wrapped states WeakMap.
*/
export const transferCodeBlockWrappedValue = (oldCodeBlockNode, newCodeBlockNode) => {
if (expValEquals('platform_editor_code_block_fold_gutter', 'isEnabled', true)) {
transferCodeBlockFoldValue(oldCodeBlockNode, newCodeBlockNode);
}
// Don't overwrite the value for the new node if it already exists.
// This can happen when a drag&drop is swapping nodes.
if (codeBlockWrappedStates.has(newCodeBlockNode)) {
return;
}
const previousValue = isCodeBlockWordWrapEnabled(oldCodeBlockNode);
codeBlockWrappedStates.set(newCodeBlockNode, previousValue);
codeBlockWrappedStates.delete(oldCodeBlockNode);
};
/**
* Swap the old node key with the new node key in the fold states WeakMap.
*/
const transferCodeBlockFoldValue = (oldCodeBlockNode, newCodeBlockNode) => {
// Don't overwrite the value for the new node if it already exists.
// This can happen when a drag&drop is swapping nodes.
if (codeBlockFoldStates.has(newCodeBlockNode)) {
return;
}
const previousValue = getCodeBlockFoldState(oldCodeBlockNode);
codeBlockFoldStates.set(newCodeBlockNode, previousValue);
codeBlockFoldStates.delete(oldCodeBlockNode);
};
/**
* As the code block node is used as the wrapped state key, there is instances where the node will be destroyed & recreated and is no longer a valid key.
* In these instances, we must get the value from that old node and set it to the value of the new node.
* This function takes all the given nodes, finds their old nodes from the old state and updates these old node keys.
*/
export const updateCodeBlockWrappedStateNodeKeys = (newCodeBlockNodes, oldState) => {
newCodeBlockNodes.forEach(newCodeBlockNode => {
if (expValEquals('platform_editor_code_block_fold_gutter', 'isEnabled', true)) {
updateCodeBlockFoldStateNodeKeys(newCodeBlockNode, oldState);
}
// Don't overwrite the value for the new node if it already exists.
// This can happen when a drag&drop is swapping nodes.
if (codeBlockWrappedStates.has(newCodeBlockNode.node)) {
return;
}
// Do not go out of range on the oldState doc. Happens on initial load.
if (oldState.doc.content.size <= newCodeBlockNode.pos) {
return;
}
const oldCodeBlockNode = oldState.doc.nodeAt(newCodeBlockNode.pos);
if (!oldCodeBlockNode || oldCodeBlockNode.type !== oldState.schema.nodes.codeBlock) {
return;
}
const previousValue = isCodeBlockWordWrapEnabled(oldCodeBlockNode);
codeBlockWrappedStates.set(newCodeBlockNode.node, previousValue);
});
};
/**
* As the code block node is used as the fold state key, there is instances where the node will be destroyed & recreated and is no longer a valid key.
* In these instances, we must get the value from that old node and set it to the value of the new node.
* This function takes all the given nodes, finds their old nodes from the old state and updates these old node keys.
*/
const updateCodeBlockFoldStateNodeKeys = (newCodeBlockNode, oldState) => {
// Don't overwrite the value for the new node if it already exists.
// This can happen when a drag&drop is swapping nodes.
if (codeBlockFoldStates.has(newCodeBlockNode.node)) {
return;
}
// Do not go out of range on the oldState doc. Happens on initial load.
if (oldState.doc.content.size <= newCodeBlockNode.pos) {
return;
}
const oldCodeBlockNode = oldState.doc.nodeAt(newCodeBlockNode.pos);
if (!oldCodeBlockNode || oldCodeBlockNode.type !== oldState.schema.nodes.codeBlock) {
return;
}
const previousValue = getCodeBlockFoldState(oldCodeBlockNode);
codeBlockFoldStates.set(newCodeBlockNode.node, previousValue);
};