Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 103x 103x 103x 2472x 206x 2266x 103x 103x 20x 20x 20x 41x 81x 41x 20x 20x 20x 20x 20x 20x 20x 8x 41x 10x 13x 31x 21x 21x 35x 1x 20x 9x 23x 9x 9x | export type FormulaSection = {
id: number | string;
type: "text" | "unit" | "prop";
displayText: string;
value: string;
};
const MENTION_MARKUP_PATTERN = /@\[([^\]]+)\]\(([^)]+)\)/;
// String.split includes every captured group in its output. The split regex
// needs one capture for the whole mention token, but not the display/id
// subgroups used by parseMentionMarkup.
function withoutCaptureGroups(patternSource: string): string {
let source = "";
for (let index = 0; index < patternSource.length; index++) {
const char = patternSource[index];
if (
char === "(" &&
patternSource[index - 1] !== "\\" &&
patternSource[index + 1] !== "?"
) {
source += "(?:";
} else {
source += char;
}
}
return source;
}
const MENTION_MARKUP_SPLIT_PATTERN = new RegExp(
`(${withoutCaptureGroups(MENTION_MARKUP_PATTERN.source)})`,
"g",
);
function parseMentionMarkup(value: string): {
displayText: string;
idType: string;
} | null {
const match = value.match(MENTION_MARKUP_PATTERN);
Iif (!match) return null;
return {
displayText: match[1],
idType: match[2],
};
}
export function parseFormulaSections(value: string): FormulaSection[] {
// Keep react-mentions markup intact for persistence, while still tracking
// plain sections for the existing "select unit, then select property" flow.
let sections = value.split(MENTION_MARKUP_SPLIT_PATTERN);
sections = sections.filter((section) => section !== "");
const parsedSections: FormulaSection[] = sections.map((section) => {
if (section.startsWith("@[")) {
const mention = parseMentionMarkup(section);
const displayText = mention?.displayText ?? section;
const idType = mention?.idType;
const type = idType?.startsWith("unit") ? "unit" : "prop";
const id = idType?.replace(type, "") ?? "";
return { id, type, value: section, displayText };
}
return { id: -1, type: "text", value: section, displayText: section };
});
if (
parsedSections.length > 0 &&
parsedSections[parsedSections.length - 1].type !== "text"
) {
parsedSections.push({ id: -1, type: "text", value: "", displayText: "" });
}
return parsedSections;
}
export function markupToPlainText(value: string): string {
return parseFormulaSections(value)
.map((section) => section.displayText)
.join("");
}
export function mapPlainTextIndexToMarkup(
value: string,
plainTextIndex: number,
): number {
let markupIndex = 0;
let currentPlainTextIndex = 0;
const mentionPattern = new RegExp(MENTION_MARKUP_PATTERN.source, "y");
while (markupIndex < value.length) {
mentionPattern.lastIndex = markupIndex;
const mention = mentionPattern.exec(value);
if (mention) {
const displayText = parseMentionMarkup(mention[0])?.displayText ?? "";
if (plainTextIndex <= currentPlainTextIndex + displayText.length) {
return markupIndex + mention[0].length;
}
currentPlainTextIndex += displayText.length;
markupIndex += mention[0].length;
continue;
}
if (currentPlainTextIndex === plainTextIndex) {
return markupIndex;
}
currentPlainTextIndex += 1;
markupIndex += 1;
}
return value.length;
}
export function getFormulaSectionIndex(
sections: FormulaSection[],
cursorPosition: number,
): number {
if (sections.length === 0) return 0;
let totalSectionLengths = 0;
for (let i = 0; i < sections.length; i++) {
totalSectionLengths += sections[i].displayText.length;
if (cursorPosition < totalSectionLengths) {
return i;
}
}
return sections.length - 1;
}
export function inferPlainTextChangeCursorPosition(
previousValue: string,
nextValue: string,
fallbackCursorPosition: number,
): number {
let prefixLength = 0;
while (
prefixLength < previousValue.length &&
prefixLength < nextValue.length &&
previousValue[prefixLength] === nextValue[prefixLength]
) {
prefixLength += 1;
}
let suffixLength = 0;
while (
suffixLength < previousValue.length - prefixLength &&
suffixLength < nextValue.length - prefixLength &&
previousValue[previousValue.length - 1 - suffixLength] ===
nextValue[nextValue.length - 1 - suffixLength]
) {
suffixLength += 1;
}
if (
prefixLength === previousValue.length &&
prefixLength === nextValue.length
) {
return fallbackCursorPosition;
}
return nextValue.length - suffixLength;
}
|