|
1 | 1 | import type { Line } from "../../deps/scrapbox-rest.ts"; |
2 | 2 | import { |
3 | | - Change, |
4 | 3 | DeleteCommit, |
5 | 4 | InsertCommit, |
6 | 5 | Socket, |
7 | 6 | socketIO, |
8 | 7 | UpdateCommit, |
9 | | - wrap, |
10 | 8 | } from "../../deps/socket.ts"; |
11 | | -import { HeadData, pull } from "./pull.ts"; |
| 9 | +import { pull } from "./pull.ts"; |
12 | 10 | import { getCodeBlocks, TinyCodeBlock } from "./getCodeBlocks.ts"; |
13 | 11 | 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"; |
16 | 14 |
|
17 | 15 | /** コードブロックの上書きに使う情報のinterface */ |
18 | 16 | export interface CodeFile { |
@@ -150,59 +148,6 @@ function flatCodeBodies(codeBlocks: readonly TinyCodeBlock[]): Line[] { |
150 | 148 | }).flat(); |
151 | 149 | } |
152 | 150 |
|
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 | | - |
206 | 151 | /** insert行のIDと各行のインデントを修正する */ |
207 | 152 | function fixCommits( |
208 | 153 | commits: (InsertCommit | UpdateCommit | DeleteCommit)[], |
|
0 commit comments