Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browser/dom/_internal.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "../../deps/testing.ts";
import { assertEquals } from "@std/assert";
import { decode, encode } from "./_internal.ts";

Deno.test("encode()", async (t) => {
Expand Down
2 changes: 1 addition & 1 deletion browser/dom/cursor.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type BaseLine, BaseStore } from "../../deps/scrapbox.ts";
import { type BaseLine, BaseStore } from "@cosense/types/userscript";
import type { Position } from "./position.ts";
import type { Page } from "./page.d.ts";

Expand Down
2 changes: 1 addition & 1 deletion browser/dom/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { press } from "./press.ts";
import { getLineCount } from "./node.ts";
import { range } from "../../range.ts";
import { textInput } from "./dom.ts";
import { isArray, isNumber, isString } from "../../is.ts";
import { isArray, isNumber, isString } from "@core/unknownutil";
import { delay } from "@std/async/delay";

export const undo = (count = 1): void => {
Expand Down
4 changes: 2 additions & 2 deletions browser/dom/extractCodeFiles.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extractCodeFiles } from "./extractCodeFiles.ts";
import type { Line } from "../../deps/scrapbox.ts";
import { assertSnapshot } from "../../deps/testing.ts";
import type { Line } from "@cosense/types/userscript";
import { assertSnapshot } from "@std/testing/snapshot";
import sample from "./sample-lines1.json" with { type: "json" };

Deno.test("extractCodeFiles", async (t) => {
Expand Down
2 changes: 1 addition & 1 deletion browser/dom/extractCodeFiles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Line } from "../../deps/scrapbox.ts";
import type { Line } from "@cosense/types/userscript";

/** 一つのソースコードを表す */
export interface CodeFile {
Expand Down
2 changes: 1 addition & 1 deletion browser/dom/getCachedLines.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Line, Scrapbox } from "../../deps/scrapbox.ts";
import type { Line, Scrapbox } from "@cosense/types/userscript";
declare const scrapbox: Scrapbox;

let isLatestData = false;
Expand Down
53 changes: 26 additions & 27 deletions browser/dom/node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isNone, isNumber, isString } from "../../is.ts";
import { ensureArray } from "../../ensure.ts";
import { isNumber, isString, isUndefined } from "@core/unknownutil";
import { ensure, isArray } from "@core/unknownutil";
import { getCachedLines } from "./getCachedLines.ts";
import { takeInternalLines } from "./takeInternalLines.ts";
import type { BaseLine, Line } from "../../deps/scrapbox.ts";
import type { BaseLine, Line } from "@cosense/types/userscript";
import { lines } from "./dom.ts";
import * as Text from "../../text.ts";

Expand All @@ -15,7 +15,7 @@ import * as Text from "../../text.ts";
export const getLineId = <T extends HTMLElement>(
value?: number | string | T,
): string | undefined => {
if (isNone(value)) return undefined;
if (isUndefined(value)) return undefined;

// 行番号のとき
if (isNumber(value)) return getBaseLine(value)?.id;
Expand All @@ -40,7 +40,7 @@ export const getLineId = <T extends HTMLElement>(
export const getLineNo = <T extends HTMLElement>(
value?: number | string | T,
): number | undefined => {
if (isNone(value)) return undefined;
if (isUndefined(value)) return undefined;

// 行番号のとき
if (isNumber(value)) return value;
Expand All @@ -52,7 +52,7 @@ export const getLineNo = <T extends HTMLElement>(
export const getLine = <T extends HTMLElement>(
value?: number | string | T,
): Line | undefined => {
if (isNone(value)) return undefined;
if (isUndefined(value)) return undefined;

// 行番号のとき
if (isNumber(value)) return getLines()[value];
Expand All @@ -64,7 +64,7 @@ export const getLine = <T extends HTMLElement>(
export const getBaseLine = <T extends HTMLElement>(
value?: number | string | T,
): BaseLine | undefined => {
if (isNone(value)) return undefined;
if (isUndefined(value)) return undefined;

// 行番号のとき
if (isNumber(value)) return takeInternalLines()[value];
Expand All @@ -79,9 +79,9 @@ export const getLineDOM = <T extends HTMLElement>(
if (isLineDOM(value)) return value;

const id = getLineId(value);
if (isNone(id)) return id;
if (isUndefined(id)) return id;
const line = document.getElementById(`L${id}`);
if (isNone(line)) return undefined;
if (isUndefined(line)) return undefined;
return line as HTMLDivElement;
};
export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
Expand All @@ -90,15 +90,14 @@ export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
export const getLineCount = (): number => takeInternalLines().length;

export const getLines = (): readonly Line[] => {
const lines = getCachedLines();
ensureArray<Line>(lines, "scrapbox.Page.lines");
return lines;
const lines = ensure(getCachedLines(), isArray);
return lines as Line[];
};

export const getText = <T extends HTMLElement>(
value?: number | string | T,
): string | undefined => {
if (isNone(value)) return undefined;
if (isUndefined(value)) return undefined;

// 数字と文字列は行として扱う
if (isNumber(value) || isString(value)) return getBaseLine(value)?.text;
Expand All @@ -118,7 +117,7 @@ export const getText = <T extends HTMLElement>(
//中に含まれている文字の列番号を全て取得し、それに対応する文字列を返す
const chars = [] as number[];
const line = getBaseLine(value);
if (isNone(line)) return;
if (isUndefined(line)) return;
for (const dom of getChars(value)) {
chars.push(getIndex(dom));
}
Expand All @@ -127,30 +126,30 @@ export const getText = <T extends HTMLElement>(

export const getExternalLink = (dom: HTMLElement): HTMLElement | undefined => {
const link = dom.closest(".link");
if (isNone(link)) return undefined;
if (isUndefined(link)) return undefined;
return link as HTMLElement;
};
export const getInternalLink = (dom: HTMLElement): HTMLElement | undefined => {
const link = dom.closest(".page-link");
if (isNone(link)) return undefined;
if (isUndefined(link)) return undefined;
return link as HTMLElement;
};
export const getLink = (dom: HTMLElement): HTMLElement | undefined => {
const link = dom.closest(".link, .page-link");
if (isNone(link)) return undefined;
if (isUndefined(link)) return undefined;
return link as HTMLElement;
};

export const getFormula = (dom: HTMLElement): HTMLElement | undefined => {
const formula = dom.closest(".formula");
if (isNone(formula)) return undefined;
if (isUndefined(formula)) return undefined;
return formula as HTMLElement;
};
export const getNextLine = <T extends HTMLElement>(
value?: number | string | T,
): Line | undefined => {
const index = getLineNo(value);
if (isNone(index)) return undefined;
if (isUndefined(index)) return undefined;

return getLine(index + 1);
};
Expand All @@ -159,26 +158,26 @@ export const getPrevLine = <T extends HTMLElement>(
value?: number | string | T,
): Line | undefined => {
const index = getLineNo(value);
if (isNone(index)) return undefined;
if (isUndefined(index)) return undefined;

return getLine(index - 1);
};

export const getHeadLineDOM = (): HTMLDivElement | undefined => {
const line = lines()?.firstElementChild;
if (isNone(line)) return undefined;
if (isUndefined(line)) return undefined;
return line as HTMLDivElement;
};
export const getTailLineDOM = (): HTMLDivElement | undefined => {
const line = lines()?.lastElementChild;
if (isNone(line)) return undefined;
if (isUndefined(line)) return undefined;
return line as HTMLDivElement;
};
export const getIndentCount = <T extends HTMLElement>(
value?: number | string | T,
): number | undefined => {
const text = getText(value);
if (isNone(text)) return undefined;
if (isUndefined(text)) return undefined;
return Text.getIndentCount(text);
};
/** 指定した行の配下にある行の数を返す
Expand All @@ -189,7 +188,7 @@ export const getIndentLineCount = <T extends HTMLElement>(
value?: number | string | T,
): number | undefined => {
const index = getLineNo(value);
if (isNone(index)) return;
if (isUndefined(index)) return;
return Text.getIndentLineCount(index, getLines());
};

Expand All @@ -210,7 +209,7 @@ export const getIndex = (dom: HTMLSpanElement): number => {
if (!isCharDOM(dom)) throw Error("A char DOM is required.");

const index = dom.className.match(/c-(\d+)/)?.[1];
if (isNone(index)) throw Error('.char-index must have ".c-{\\d}"');
if (isUndefined(index)) throw Error('.char-index must have ".c-{\\d}"');
return parseInt(index);
};
export const getHeadCharDOM = (
Expand Down Expand Up @@ -242,7 +241,7 @@ export const getDOMFromPoint = (
const char = targets.find((target) => isCharDOM(target));
const line = targets.find((target) => isLineDOM(target));
return {
char: isNone(char) ? undefined : char as HTMLSpanElement,
line: isNone(line) ? undefined : line as HTMLDivElement,
char: isUndefined(char) ? undefined : char as HTMLSpanElement,
line: isUndefined(line) ? undefined : line as HTMLDivElement,
};
};
2 changes: 1 addition & 1 deletion browser/dom/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type PageTransitionContext,
pushPageTransition,
} from "./pushPageTransition.ts";
import type { Scrapbox } from "../../deps/scrapbox.ts";
import type { Scrapbox } from "@cosense/types/userscript";
declare const scrapbox: Scrapbox;

export interface OpenOptions {
Expand Down
4 changes: 2 additions & 2 deletions browser/dom/page.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseStore } from "../../deps/scrapbox.ts";
import type { Page as PageData } from "../../deps/scrapbox-rest.ts";
import { BaseStore } from "@cosense/types/userscript";
import type { Page as PageData } from "@cosense/types/rest";

export interface SetPositionOptions {
/** カーソルが画面外に移動したとき、カーソルが見える位置までページをスクロールするかどうか
Expand Down
2 changes: 1 addition & 1 deletion browser/dom/selection.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type BaseLine, BaseStore } from "../../deps/scrapbox.ts";
import { type BaseLine, BaseStore } from "@cosense/types/userscript";
import type { Position } from "./position.ts";

export interface Range {
Expand Down
2 changes: 1 addition & 1 deletion browser/dom/takeInternalLines.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lines } from "./dom.ts";
import type { BaseLine } from "../../deps/scrapbox.ts";
import type { BaseLine } from "@cosense/types/userscript";

/** Scrapbox内部の本文データの参照を取得する
*
Expand Down
2 changes: 1 addition & 1 deletion browser/dom/textInputEventListener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Scrapbox } from "../../deps/scrapbox.ts";
import type { Scrapbox } from "@cosense/types/userscript";
import { textInput } from "./dom.ts";
import { decode, encode } from "./_internal.ts";
declare const scrapbox: Scrapbox;
Expand Down
3 changes: 2 additions & 1 deletion browser/websocket/_codeBlock.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertEquals, assertSnapshot } from "../../deps/testing.ts";
import { assertEquals } from "@std/assert";
import { assertSnapshot } from "@std/testing/snapshot";
import { extractFromCodeTitle } from "./_codeBlock.ts";

Deno.test("extractFromCodeTitle()", async (t) => {
Expand Down
3 changes: 2 additions & 1 deletion browser/websocket/applyCommit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CommitNotification } from "../../deps/socket.ts";
import type { Line } from "../../deps/scrapbox-rest.ts";
import type { Page } from "@cosense/types/rest";
import { getUnixTimeFromId } from "./id.ts";
type Line = Page["lines"][number];

export interface ApplyCommitProp {
/** changesの作成日時
Expand Down
6 changes: 3 additions & 3 deletions browser/websocket/deletePage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { push, type PushOptions, type RetryError } from "./push.ts";
import type { Result } from "../../rest/util.ts";
import { push, type PushError, type PushOptions } from "./push.ts";
import type { Result } from "option-t/plain_result";

export type DeletePageOptions = PushOptions;

Expand All @@ -13,7 +13,7 @@ export const deletePage = (
project: string,
title: string,
options?: DeletePageOptions,
): Promise<Result<string, RetryError>> =>
): Promise<Result<string, PushError>> =>
push(
project,
title,
Expand Down
2 changes: 1 addition & 1 deletion browser/websocket/diffToChanges.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { diffToChanges } from "./diffToChanges.ts";
import { assertEquals } from "../../deps/testing.ts";
import { assertEquals } from "@std/assert";

Deno.test("diffToChanges()", async ({ step }) => {
const userId = "xxxyyy";
Expand Down
2 changes: 1 addition & 1 deletion browser/websocket/diffToChanges.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { diff, toExtendedChanges } from "../../deps/onp.ts";
import type { Line } from "../../deps/scrapbox.ts";
import type { Line } from "@cosense/types/userscript";
import type {
DeleteChange,
InsertChange,
Expand Down
3 changes: 2 additions & 1 deletion browser/websocket/findMetadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { findMetadata, getHelpfeels } from "./findMetadata.ts";
import { assertEquals, assertSnapshot } from "../../deps/testing.ts";
import { assertEquals } from "@std/assert";
import { assertSnapshot } from "@std/testing/snapshot";

const text = `てすと
[ふつうの]リンク
Expand Down
3 changes: 2 additions & 1 deletion browser/websocket/findMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type BaseLine, type Node, parse } from "../../deps/scrapbox.ts";
import { type Node, parse } from "@progfay/scrapbox-parser";
import type { BaseLine } from "@cosense/types/userscript";
import { toTitleLc } from "../../title.ts";
import { parseYoutube } from "../../parser/youtube.ts";

Expand Down
32 changes: 0 additions & 32 deletions browser/websocket/id.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
import { getProject } from "../../rest/project.ts";
import { getProfile } from "../../rest/profile.ts";

/** cached user ID */
let userId: string | undefined;
export const getUserId = async (): Promise<string> => {
if (userId !== undefined) return userId;

const user = await getProfile();
if (user.isGuest) {
throw new Error("this script can only be executed by Logged in users");
}
userId = user.id;
return userId;
};

/** cached pairs of project name and project id */
const projectMap = new Map<string, string>();
export const getProjectId = async (project: string): Promise<string> => {
const cachedId = projectMap.get(project);
if (cachedId !== undefined) return cachedId;

const result = await getProject(project);
if (!result.ok) {
const { name, message } = result.value;
throw new Error(`${name} ${message}`);
}
const { id } = result.value;
projectMap.set(project, id);
return id;
};

const zero = (n: string) => n.padStart(8, "0");

export const createNewLineId = (userId: string): string => {
Expand Down
2 changes: 1 addition & 1 deletion browser/websocket/isSameArray.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isSameArray } from "./isSameArray.ts";
import { assert } from "../../deps/testing.ts";
import { assert } from "@std/assert";

Deno.test("isSameArray()", () => {
assert(isSameArray([1, 2, 3], [1, 2, 3]));
Expand Down
2 changes: 1 addition & 1 deletion browser/websocket/isSimpleCodeFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertFalse } from "../../deps/testing.ts";
import { assert, assertFalse } from "@std/assert";
import { isSimpleCodeFile } from "./isSimpleCodeFile.ts";
import type { SimpleCodeFile } from "./updateCodeFile.ts";

Expand Down
Loading