diff --git a/browser/websocket/room.ts b/browser/websocket/room.ts index a448a29..04be068 100644 --- a/browser/websocket/room.ts +++ b/browser/websocket/room.ts @@ -4,32 +4,14 @@ import { socketIO, wrap, } from "../../deps/socket.ts"; -import { createNewLineId, getProjectId, getUserId } from "./id.ts"; +import { getProjectId, getUserId } from "./id.ts"; import { diffToChanges } from "./patch.ts"; import { applyCommit } from "./applyCommit.ts"; import type { Line } from "../../deps/scrapbox.ts"; -import { ensureEditablePage, pushCommit, pushWithRetry } from "./_fetch.ts"; +import { ensureEditablePage, pushCommit } from "./_fetch.ts"; export type { CommitNotification }; export interface JoinPageRoomResult { - /** 特定の位置にテキストを挿入する - * - * @param text - 挿入したいテキスト (複数行も可) - * @param beforeId - この行IDが指し示す行の上に挿入する。末尾に挿入する場合は`_end`を指定する - */ - insert: (text: string, beforeId: string) => Promise; - /** 特定の行を削除する - * - * @param lineId 削除したい行の行ID - */ - remove: (lineId: string) => Promise; - /** 特定の位置のテキストを書き換える - * - * @param text - 書き換え後のテキスト (改行は不可) - * @param lineId - 書き換えたい行の行ID - */ - update: (text: string, lineId: string) => Promise; - /** ページ全体を書き換える * * `update()`で現在の本文から書き換え後の本文を作ってもらう。 @@ -86,49 +68,7 @@ export async function joinPageRoom( } })(); - async function push(changes: Change[], retry = 3) { - // 変更後のlinesを計算する - const changedLines = applyCommit(lines, changes, { - userId, - }); - // タイトルの変更チェック - // 空ページの場合もタイトル変更commitを入れる - if (lines[0].text !== changedLines[0].text || !created) { - changes.push({ title: changedLines[0].text }); - } - // サムネイルの変更チェック - const oldDescriptions = lines.slice(1, 6).map((line) => line.text); - const newDescriptions = changedLines.slice(1, 6).map((lines) => lines.text); - if (oldDescriptions.join("\n") !== newDescriptions.join("\n")) { - changes.push({ descriptions: newDescriptions }); - } - - // serverにpushする - parentId = await pushWithRetry(request, changes, { - parentId, - projectId, - pageId, - userId, - project, - title, - retry, - }); - // pushに成功したら、localにも変更を反映する - created = true; - lines = changedLines; - } - return { - insert: async (text: string, beforeId = "_end") => { - const changes = text.split(/\n|\r\n/).map((line) => ({ - _insert: beforeId, - lines: { text: line, id: createNewLineId(userId) }, - })); - await push(changes); - }, - remove: (lineId: string) => push([{ _delete: lineId, lines: -1 }]), - update: (text: string, lineId: string) => - push([{ _update: lineId, lines: { text } }]), patch: async (update: (before: Line[]) => string[] | Promise) => { const tryPush = async () => { const pending = update(lines);