Skip to content

Commit c563d15

Browse files
committed
updateCodeFile.tsの一部函数を_codeBlock.tsへ分割
これらの函数はupdateCodeBlock.tsでも使用する可能性があるため
1 parent 626fb4a commit c563d15

File tree

2 files changed

+61
-58
lines changed

2 files changed

+61
-58
lines changed

browser/websocket/_codeBlock.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Change, InsertCommit, Socket, wrap } from "../../deps/socket.ts";
2+
import { HeadData } from "./pull.ts";
3+
import { createNewLineId, getProjectId, getUserId } from "./id.ts";
4+
import { CodeFile } from "./updateCodeFile.ts";
5+
import { pushWithRetry } from "./_fetch.ts";
6+
7+
/** コミットを送信する一連の処理 */
8+
export async function applyCommit(
9+
commits: Change[],
10+
head: HeadData,
11+
projectName: string,
12+
pageTitle: string,
13+
socket: Socket,
14+
): ReturnType<typeof pushWithRetry> {
15+
const [projectId, userId] = await Promise.all([
16+
getProjectId(projectName),
17+
getUserId(),
18+
]);
19+
const { request } = wrap(socket);
20+
return await pushWithRetry(request, commits, {
21+
parentId: head.commitId,
22+
projectId: projectId,
23+
pageId: head.pageId,
24+
userId: userId,
25+
project: projectName,
26+
title: pageTitle,
27+
retry: 3,
28+
});
29+
}
30+
31+
/** 新規コードブロックのコミットを作成する */
32+
export async function makeCommitsNewCodeBlock(
33+
code: CodeFile,
34+
insertLineId: string,
35+
): Promise<InsertCommit[]> {
36+
const userId = await getUserId();
37+
const codeName = code.filename + (code.lang ? `(${code.lang})` : "");
38+
const codeBody = Array.isArray(code.content)
39+
? code.content
40+
: code.content.split("\n");
41+
const commits: InsertCommit[] = [{
42+
_insert: insertLineId,
43+
lines: {
44+
id: createNewLineId(userId),
45+
text: `code:${codeName}`,
46+
},
47+
}];
48+
for (const bodyLine of codeBody) {
49+
commits.push({
50+
_insert: insertLineId,
51+
lines: {
52+
id: createNewLineId(userId),
53+
text: " " + bodyLine,
54+
},
55+
});
56+
}
57+
return commits;
58+
}

browser/websocket/updateCodeFile.ts

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import type { Line } from "../../deps/scrapbox-rest.ts";
22
import {
3-
Change,
43
DeleteCommit,
54
InsertCommit,
65
Socket,
76
socketIO,
87
UpdateCommit,
9-
wrap,
108
} from "../../deps/socket.ts";
11-
import { HeadData, pull } from "./pull.ts";
9+
import { pull } from "./pull.ts";
1210
import { getCodeBlocks, TinyCodeBlock } from "./getCodeBlocks.ts";
1311
import { diffToChanges } from "./diffToChanges.ts";
14-
import { createNewLineId, getProjectId, getUserId } from "./id.ts";
15-
import { pushWithRetry } from "./_fetch.ts";
12+
import { getUserId } from "./id.ts";
13+
import { applyCommit, makeCommitsNewCodeBlock } from "./_codeBlock.ts";
1614

1715
/** コードブロックの上書きに使う情報のinterface */
1816
export interface CodeFile {
@@ -150,59 +148,6 @@ function flatCodeBodies(codeBlocks: readonly TinyCodeBlock[]): Line[] {
150148
}).flat();
151149
}
152150

153-
/** コミットを送信する一連の処理 */
154-
async function applyCommit(
155-
commits: Change[],
156-
head: HeadData,
157-
projectName: string,
158-
pageTitle: string,
159-
socket: Socket,
160-
): ReturnType<typeof pushWithRetry> {
161-
const [projectId, userId] = await Promise.all([
162-
getProjectId(projectName),
163-
getUserId(),
164-
]);
165-
const { request } = wrap(socket);
166-
return await pushWithRetry(request, commits, {
167-
parentId: head.commitId,
168-
projectId: projectId,
169-
pageId: head.pageId,
170-
userId: userId,
171-
project: projectName,
172-
title: pageTitle,
173-
retry: 3,
174-
});
175-
}
176-
177-
/** 新規コードブロックのコミットを作成する */
178-
async function makeCommitsNewCodeBlock(
179-
code: CodeFile,
180-
insertLineId: string,
181-
): Promise<InsertCommit[]> {
182-
const userId = await getUserId();
183-
const codeName = code.filename + (code.lang ? `(${code.lang})` : "");
184-
const codeBody = Array.isArray(code.content)
185-
? code.content
186-
: code.content.split("\n");
187-
const commits: InsertCommit[] = [{
188-
_insert: insertLineId,
189-
lines: {
190-
id: createNewLineId(userId),
191-
text: `code:${codeName}`,
192-
},
193-
}];
194-
for (const bodyLine of codeBody) {
195-
commits.push({
196-
_insert: insertLineId,
197-
lines: {
198-
id: createNewLineId(userId),
199-
text: " " + bodyLine,
200-
},
201-
});
202-
}
203-
return commits;
204-
}
205-
206151
/** insert行のIDと各行のインデントを修正する */
207152
function fixCommits(
208153
commits: (InsertCommit | UpdateCommit | DeleteCommit)[],

0 commit comments

Comments
 (0)