-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,937 additions
and
1,801 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,3 @@ | |
# Dist | ||
node_modules | ||
dist/ | ||
|
||
# IDE | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"editor.defaultFormatter": "biomejs.biome", | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"version": "unknown", | ||
"buildTime": "unknown" | ||
"version": "unknown", | ||
"buildTime": "unknown" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,94 @@ | ||
import type { StatefulAnchorEditorInterface, Anchor, Step, NeighborResult } from "./interface"; | ||
import { AnchorQuery } from "./query"; | ||
import type { | ||
Anchor, | ||
NeighborResult, | ||
StatefulAnchorEditorInterface, | ||
Step, | ||
} from './interface'; | ||
import { AnchorQuery } from './query'; | ||
|
||
|
||
|
||
export class AnchorEditor extends AnchorQuery implements StatefulAnchorEditorInterface { | ||
lastMoveDirection: "left" | "right" | "up" | "down" | "none" = "none"; | ||
lastMoveAnchor: "start" | "end" | "none" = "none"; | ||
// constructor(config: LocConfig, root: HTMLElement) { | ||
// super(config, root); | ||
// } | ||
resetAnchor(anchor: Anchor): Anchor | null { | ||
const selection = document.getSelection(); | ||
if (selection) { | ||
selection.setPosition(anchor.container, anchor.offset); | ||
this.lastMoveDirection = "none"; | ||
this.lastMoveAnchor = "none"; | ||
return anchor; | ||
} | ||
return null; | ||
export class AnchorEditor | ||
extends AnchorQuery | ||
implements StatefulAnchorEditorInterface | ||
{ | ||
lastMoveDirection: 'left' | 'right' | 'up' | 'down' | 'none' = 'none'; | ||
lastMoveAnchor: 'start' | 'end' | 'none' = 'none'; | ||
// constructor(config: LocConfig, root: HTMLElement) { | ||
// super(config, root); | ||
// } | ||
resetAnchor(anchor: Anchor): Anchor | null { | ||
const selection = document.getSelection(); | ||
if (selection) { | ||
selection.setPosition(anchor.container, anchor.offset); | ||
this.lastMoveDirection = 'none'; | ||
this.lastMoveAnchor = 'none'; | ||
return anchor; | ||
} | ||
resetStartAnchor(anchor: Anchor): Anchor | null { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
range.setStart(anchor.container, anchor.offset); | ||
this.lastMoveDirection = "none"; | ||
this.lastMoveAnchor = "start"; | ||
return anchor; | ||
} | ||
return null; | ||
return null; | ||
} | ||
resetStartAnchor(anchor: Anchor): Anchor | null { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
range.setStart(anchor.container, anchor.offset); | ||
this.lastMoveDirection = 'none'; | ||
this.lastMoveAnchor = 'start'; | ||
return anchor; | ||
} | ||
resetEndAnchor(anchor: Anchor): Anchor | null { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
range.setEnd(anchor.container, anchor.offset); | ||
this.lastMoveDirection = "none"; | ||
this.lastMoveAnchor = "end"; | ||
return anchor; | ||
} | ||
return null; | ||
return null; | ||
} | ||
resetEndAnchor(anchor: Anchor): Anchor | null { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
range.setEnd(anchor.container, anchor.offset); | ||
this.lastMoveDirection = 'none'; | ||
this.lastMoveAnchor = 'end'; | ||
return anchor; | ||
} | ||
return null; | ||
} | ||
|
||
|
||
resetRange(start: Anchor, end: Anchor): boolean { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
range.setStart(start.container, start.offset); | ||
range.setEnd(end.container, end.offset); | ||
this.lastMoveDirection = "none"; | ||
this.lastMoveAnchor = "none"; | ||
return true; | ||
} | ||
return false; | ||
resetRange(start: Anchor, end: Anchor): boolean { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
range.setStart(start.container, start.offset); | ||
range.setEnd(end.container, end.offset); | ||
this.lastMoveDirection = 'none'; | ||
this.lastMoveAnchor = 'none'; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
|
||
moveEndAnchorTo(step: Step): NeighborResult { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
const result = this.getHorizontalAnchor({ | ||
anchor: { container: range.endContainer, offset: range.endOffset }, | ||
step: step, | ||
}); | ||
if (result.next) { | ||
this.resetEndAnchor(result.next); | ||
this.lastMoveDirection = step.direction; | ||
this.lastMoveAnchor = "end"; | ||
return result; | ||
} | ||
} | ||
throw new Error("no selection"); | ||
moveEndAnchorTo(step: Step): NeighborResult { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
const result = this.getHorizontalAnchor({ | ||
anchor: { container: range.endContainer, offset: range.endOffset }, | ||
step: step, | ||
}); | ||
if (result.next) { | ||
this.resetEndAnchor(result.next); | ||
this.lastMoveDirection = step.direction; | ||
this.lastMoveAnchor = 'end'; | ||
return result; | ||
} | ||
} | ||
throw new Error('no selection'); | ||
} | ||
|
||
moveStartAnchorTo(step: Step): NeighborResult { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
const result = this.getHorizontalAnchor({ | ||
anchor: { container: range.startContainer, offset: range.startOffset }, | ||
step: step, | ||
}); | ||
if (result.next) { | ||
this.resetStartAnchor(result.next); | ||
this.lastMoveDirection = step.direction; | ||
this.lastMoveAnchor = "start"; | ||
} | ||
return result; | ||
} | ||
throw new Error("no selection"); | ||
moveStartAnchorTo(step: Step): NeighborResult { | ||
const range = document.getSelection()?.getRangeAt(0); | ||
if (range) { | ||
const result = this.getHorizontalAnchor({ | ||
anchor: { container: range.startContainer, offset: range.startOffset }, | ||
step: step, | ||
}); | ||
if (result.next) { | ||
this.resetStartAnchor(result.next); | ||
this.lastMoveDirection = step.direction; | ||
this.lastMoveAnchor = 'start'; | ||
} | ||
return result; | ||
} | ||
throw new Error('no selection'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,48 @@ | ||
import { NodeToString, anchorToStrong } from "./helper"; | ||
import type { Anchor } from "./interface"; | ||
import { NodeToString, anchorToStrong } from './helper'; | ||
import type { Anchor } from './interface'; | ||
|
||
export enum ErrorCode { | ||
INVALID_NODE = "INVALID_NODE", | ||
INVALID_ANCHOR = "INVALID_ANCHOR", | ||
INVALID_DIRECTION = "INVALID_DIRECTION", | ||
INVALID_OFFSET = "INVALID_OFFSET", | ||
AT_BOUNDARY = "AT_BOUNDARY", | ||
PARENT_NOT_FOUND = "PARENT_NOT_FOUND", | ||
CALCULATION_ERROR = "CALCULATION_ERROR", | ||
} | ||
|
||
export interface ErrorLocation { | ||
clazz: string; | ||
method: string; | ||
case?: string; | ||
} | ||
|
||
INVALID_NODE = 'INVALID_NODE', | ||
INVALID_ANCHOR = 'INVALID_ANCHOR', | ||
INVALID_DIRECTION = 'INVALID_DIRECTION', | ||
INVALID_OFFSET = 'INVALID_OFFSET', | ||
AT_BOUNDARY = 'AT_BOUNDARY', | ||
PARENT_NOT_FOUND = 'PARENT_NOT_FOUND', | ||
CALCULATION_ERROR = 'CALCULATION_ERROR', | ||
} | ||
|
||
export interface QueryError { | ||
code: ErrorCode; | ||
message: string; | ||
location: ErrorLocation; | ||
context?: { [key: string]: unknown }; | ||
} | ||
|
||
export interface ErrorLocation { | ||
clazz: string; | ||
method: string; | ||
case?: string; | ||
} | ||
|
||
export interface QueryError { | ||
code: ErrorCode; | ||
message: string; | ||
location: ErrorLocation; | ||
context?: { [key: string]: unknown }; | ||
} | ||
|
||
export class InvalidAnchorError extends Error { | ||
constructor(anchor: Anchor) { | ||
super(`Anchor: \n ${anchorToStrong(anchor)}`); | ||
this.name = "InvalidAnchorError"; | ||
} | ||
constructor(anchor: Anchor) { | ||
super(`Anchor: \n ${anchorToStrong(anchor)}`); | ||
this.name = 'InvalidAnchorError'; | ||
} | ||
} | ||
|
||
export class InvalidBoundaryDirectionError extends Error { | ||
constructor(container: Node, direction: string) { | ||
super(`Invalid boundary direction ${direction} in node: \n${NodeToString(container)}`); | ||
this.name = "InvalidBoundaryDirectionError"; | ||
} | ||
constructor(container: Node, direction: string) { | ||
super( | ||
`Invalid boundary direction ${direction} in node: \n${NodeToString(container)}`, | ||
); | ||
this.name = 'InvalidBoundaryDirectionError'; | ||
} | ||
} | ||
|
||
export class InvalidNodeTypeError extends Error { | ||
constructor(node: Node) { | ||
super(`Invalid node type ${node.nodeName}`); | ||
this.name = "InvalidNodeTypeError"; | ||
} | ||
} | ||
constructor(node: Node) { | ||
super(`Invalid node type ${node.nodeName}`); | ||
this.name = 'InvalidNodeTypeError'; | ||
} | ||
} |
Oops, something went wrong.