mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 14:01:00 +00:00
refactor(desktop): reuse attribute text ancestors (#16499)
This commit is contained in:
1 parent
19c113a893
commit
86324f6cc8
1 file changed
+16
-34
@@ -24,16 +24,6 @@ const isFormattingAttribute = (key: string) =>
|
||||
key.split(":").at(-1) ?? key,
|
||||
);
|
||||
|
||||
/** An element's text from its text nodes, or from an attribute's values, at a pointer in a view. */
|
||||
const textAt = (
|
||||
view: JsonValue | null,
|
||||
pointer: string,
|
||||
attribute?: string,
|
||||
) => {
|
||||
const element = resolvePointer(view, pointer, "xml");
|
||||
return isXmlElement(element) ? elementText(element, attribute) : undefined;
|
||||
};
|
||||
|
||||
/** Whether an element holding an attribute is a record rather than a run of text: it carries an identifier or a name, as Umbrello's `<ownedAttribute xmi:id="…" name="email" visibility="private"/>`, beside it. */
|
||||
const isNamedRecord = (node: JsonValue, key: string) =>
|
||||
isXmlElement(node) &&
|
||||
@@ -43,18 +33,17 @@ const isNamedRecord = (node: JsonValue, key: string) =>
|
||||
);
|
||||
|
||||
/**
|
||||
* The elements below the element at a pointer that hold an attribute, as
|
||||
* The elements below a selected element that hold an attribute, as
|
||||
* runs do: none holding another, none carrying an identifier or a name
|
||||
* (`isNamedRecord`), and the element itself holding none; a Freeplane node
|
||||
* and the nodes in it, each holding `TEXT`, are no runs.
|
||||
*/
|
||||
const runsOf = (view: JsonValue | null, pointer: string, key: string) => {
|
||||
const element = resolvePointer(view, pointer, "xml");
|
||||
const runsOf = (element: JsonValue, key: string) => {
|
||||
const holds = (node: JsonValue) =>
|
||||
isXmlElement(node) && typeof node.attributes[key] === "string";
|
||||
let runs = 0;
|
||||
const pending: { node: JsonValue; inside: boolean }[] = [
|
||||
{ node: element ?? null, inside: false },
|
||||
{ node: element, inside: false },
|
||||
];
|
||||
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
||||
const { node, inside } = next;
|
||||
@@ -68,22 +57,6 @@ const runsOf = (view: JsonValue | null, pointer: string, key: string) => {
|
||||
return runs;
|
||||
};
|
||||
|
||||
/** Whether an element keeps its text in runs of an attribute at both ends: no text nodes, and its holders runs (`runsOf`), two or more or one holding words with a space. */
|
||||
const holdsRuns = (
|
||||
{ ends }: AttributeTextContext,
|
||||
pointer: string,
|
||||
key: string,
|
||||
) => {
|
||||
const views = [ends.baseline, ends.final];
|
||||
const runs = views.map((view) => runsOf(view, pointer, key));
|
||||
return (
|
||||
runs.every((count) => count !== undefined) &&
|
||||
(runs.some((count) => (count ?? 0) > 1) ||
|
||||
/\s/u.test(textAt(ends.final, pointer, key) ?? "")) &&
|
||||
views.every((view) => textAt(view, pointer)?.trim() === "")
|
||||
);
|
||||
};
|
||||
|
||||
/** What an attribute-text check reads: a file's two ends, whether the final view decides a check at a pointer, and the longest text it compares. */
|
||||
export interface AttributeTextContext {
|
||||
readonly file: string;
|
||||
@@ -135,11 +108,20 @@ export const attributeTextCheck = (
|
||||
}
|
||||
if (ancestor === undefined || xmlParentPointer(ancestor) === undefined)
|
||||
return undefined;
|
||||
const after = textAt(ends.final, ancestor, key);
|
||||
return holdsRuns(context, ancestor, key) &&
|
||||
const beforeElement = resolvePointer(ends.baseline, ancestor, "xml");
|
||||
const afterElement = resolvePointer(ends.final, ancestor, "xml");
|
||||
if (!isXmlElement(beforeElement) || !isXmlElement(afterElement))
|
||||
return undefined;
|
||||
const after = elementText(afterElement, key);
|
||||
const beforeRuns = runsOf(beforeElement, key);
|
||||
const afterRuns = runsOf(afterElement, key);
|
||||
return beforeRuns !== undefined &&
|
||||
afterRuns !== undefined &&
|
||||
(beforeRuns > 1 || afterRuns > 1 || /\s/u.test(after)) &&
|
||||
elementText(beforeElement).trim() === "" &&
|
||||
elementText(afterElement).trim() === "" &&
|
||||
context.named(ancestor) &&
|
||||
after !== undefined &&
|
||||
after !== textAt(ends.baseline, ancestor, key) &&
|
||||
after !== elementText(beforeElement, key) &&
|
||||
after.length <= context.chars
|
||||
? {
|
||||
element: ancestor,
|
||||
|
||||
Reference in new issue
Block a user