mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00

* Update to latest Node LTS * Add sveltekit * Split tslib into separate @generated and @tslib components SvelteKit's path aliases don't support multiple locations, so our old approach of using @tslib to refer to both ts/lib and out/ts/lib will no longer work. Instead, all generated sources and their includes are placed in a separate out/ts/generated folder, and imported via @generated instead. This also allows us to generate .ts files, instead of needing to output separate .d.ts and .js files. * Switch package.json to module type * Avoid usage of baseUrl Incompatible with SvelteKit * Move sass into ts; use relative links SvelteKit's default sass support doesn't allow overriding loadPaths * jest->vitest, graphs example working with yarn dev * most pages working in dev mode * Some fixes after rebasing * Fix/silence some svelte-check errors * Get image-occlusion working with Fabric types * Post-rebase lock changes * Editor is now checked * SvelteKit build integrated into ninja * Use the new SvelteKit entrypoint for pages like congrats/deck options/etc * Run eslint once for ts/**; fix some tests * Fix a bunch of issues introduced when rebasing over latest main * Run eslint fix * Fix remaining eslint+pylint issues; tests now all pass * Fix some issues with a clean build * Latest bufbuild no longer requires @__PURE__ hack * Add a few missed dependencies * Add yarn.bat to fix Windows build * Fix pages failing to show when ANKI_API_PORT not defined * Fix svelte-check and vitest on Windows * Set node path in ./yarn * Move svelte-kit output to ts/.svelte-kit Sadly, I couldn't figure out a way to store it in out/ if out/ is a symlink, as it breaks module resolution when SvelteKit is run. * Allow HMR inside Anki * Skip SvelteKit build when HMR is defined * Fix some post-rebase issues I should have done a normal merge instead.
120 lines
3.3 KiB
TypeScript
120 lines
3.3 KiB
TypeScript
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import { nodeIsComment, nodeIsElement, nodeIsText } from "@tslib/dom";
|
|
import { ascend } from "@tslib/node";
|
|
|
|
/**
|
|
* Represents a subset of DOM ranges which can be called with `.surroundContents()`.
|
|
*/
|
|
export class FlatRange {
|
|
private constructor(
|
|
public parent: Node,
|
|
public startIndex: number,
|
|
public endIndex: number,
|
|
) {}
|
|
|
|
/**
|
|
* The new flat range does not represent the range itself but
|
|
* rather a possible new node that surrounds the boundary points
|
|
* (node, start) till (node, end).
|
|
*
|
|
* @remarks
|
|
* Indices should be >= 0 and startIndex <= endIndex.
|
|
*/
|
|
static make(node: Node, startIndex: number, endIndex = startIndex + 1): FlatRange {
|
|
return new FlatRange(node, startIndex, endIndex);
|
|
}
|
|
|
|
/**
|
|
* @remarks
|
|
* Must be sibling flat ranges.
|
|
*/
|
|
static merge(before: FlatRange, after: FlatRange): FlatRange {
|
|
return FlatRange.make(before.parent, before.startIndex, after.endIndex);
|
|
}
|
|
|
|
/**
|
|
* @remarks
|
|
*/
|
|
static fromNode(node: Node): FlatRange {
|
|
const parent = ascend(node);
|
|
const index = Array.prototype.indexOf.call(parent.childNodes, node);
|
|
|
|
return FlatRange.make(parent, index);
|
|
}
|
|
|
|
get firstChild(): ChildNode {
|
|
return this.parent.childNodes[this.startIndex];
|
|
}
|
|
|
|
get lastChild(): ChildNode {
|
|
return this.parent.childNodes[this.endIndex - 1];
|
|
}
|
|
|
|
/**
|
|
* @see `fromNode`
|
|
*/
|
|
select(node: Node): void {
|
|
this.parent = ascend(node);
|
|
this.startIndex = Array.prototype.indexOf.call(this.parent.childNodes, node);
|
|
this.endIndex = this.startIndex + 1;
|
|
}
|
|
|
|
toDOMRange(): Range {
|
|
const range = new Range();
|
|
range.setStart(this.parent, this.startIndex);
|
|
range.setEnd(this.parent, this.endIndex);
|
|
|
|
if (range.collapsed) {
|
|
// If the range is collapsed to a single element, move the range inside the element.
|
|
// This prevents putting the surround above the base element.
|
|
const selected = range.commonAncestorContainer.childNodes[range.startOffset];
|
|
|
|
if (nodeIsElement(selected)) {
|
|
range.selectNode(selected);
|
|
}
|
|
}
|
|
|
|
return range;
|
|
}
|
|
|
|
[Symbol.iterator](): Iterator<ChildNode, null, unknown> {
|
|
const parent = this.parent;
|
|
const end = this.endIndex;
|
|
let step = this.startIndex;
|
|
|
|
return {
|
|
next(): IteratorResult<ChildNode, null> {
|
|
if (step >= end) {
|
|
return { value: null, done: true };
|
|
}
|
|
|
|
return { value: parent.childNodes[step++], done: false };
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @returns Amount of contained nodes
|
|
*/
|
|
get length(): number {
|
|
return this.endIndex - this.startIndex;
|
|
}
|
|
|
|
toString(): string {
|
|
let output = "";
|
|
|
|
for (const node of [...this]) {
|
|
if (nodeIsText(node)) {
|
|
output += node.data;
|
|
} else if (nodeIsComment(node)) {
|
|
output += `<!--${node.data}-->`;
|
|
} else if (nodeIsElement(node)) {
|
|
output += node.outerHTML;
|
|
}
|
|
}
|
|
|
|
return output;
|
|
}
|
|
}
|