Skip to content

Commit

Permalink
use biome to format code
Browse files Browse the repository at this point in the history
  • Loading branch information
sailist committed Dec 29, 2024
1 parent a34decb commit eb44d42
Show file tree
Hide file tree
Showing 17 changed files with 1,937 additions and 1,801 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,3 @@
# Dist
node_modules
dist/

# IDE
.vscode/*
!.vscode/extensions.json
.idea
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.defaultFormatter": "biomejs.biome",
"editor.formatOnSave": true
}
8 changes: 4 additions & 4 deletions document/components/playboard.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useRef, useState } from 'react';
import { AnchorQuery } from '../../src/query';
import { AnchorEditor } from '../../src/editor';
import { anchorToStrong } from '../../src/helper';
// biome-ignore lint/style/useImportType: <explanation>
import React from 'react';
import { AnchorEditor } from '../../src/editor';
import { anchorToStrong } from '../../src/helper';
import { AnchorQuery } from '../../src/query';

interface EditableDivProps {
initialContent?: string;
Expand Down Expand Up @@ -143,7 +143,7 @@ export const EditablePlay: React.FC<EditableDivProps> = ({
direction: 'right',
stride: 'char',
});

if (ret.error && intervalRef.current) {
console.log('setStartAnchorTo failed');
clearInterval(intervalRef.current);
Expand Down
4 changes: 2 additions & 2 deletions document/docs/version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "unknown",
"buildTime": "unknown"
"version": "unknown",
"buildTime": "unknown"
}
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"files": ["dist"],
"scripts": {
"build": "rslib build",
"check": "biome check --write",
Expand Down
163 changes: 83 additions & 80 deletions src/editor.ts
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');
}
}
73 changes: 36 additions & 37 deletions src/errors.ts
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';
}
}
Loading

0 comments on commit eb44d42

Please sign in to comment.