Skip to content

Commit 42cea00

Browse files
committed
コードブロック本体のインデント計算処理を分けた
1 parent 821844c commit 42cea00

File tree

2 files changed

+12
-35
lines changed

2 files changed

+12
-35
lines changed

browser/websocket/_codeBlock.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Change, InsertCommit, Socket, wrap } from "../../deps/socket.ts";
1+
import { Change, Socket, wrap } from "../../deps/socket.ts";
22
import { HeadData } from "./pull.ts";
3-
import { createNewLineId, getProjectId, getUserId } from "./id.ts";
4-
import { CodeFile } from "./updateCodeFile.ts";
3+
import { getProjectId, getUserId } from "./id.ts";
54
import { pushWithRetry } from "./_fetch.ts";
5+
import { TinyCodeBlock } from "./getCodeBlocks.ts";
66

77
/** コミットを送信する一連の処理 */
88
export async function applyCommit(
@@ -28,30 +28,10 @@ export async function applyCommit(
2828
});
2929
}
3030

31-
/** 新規コードブロックのコミットを作成する */
32-
export function* makeCommitsNewCodeBlock(
33-
code: CodeFile,
34-
insertLineId: string,
35-
{ userId }: { userId: string },
36-
): Generator<InsertCommit, void, unknown> {
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-
yield {
42-
_insert: insertLineId,
43-
lines: {
44-
id: createNewLineId(userId),
45-
text: `code:${codeName}`,
46-
},
47-
};
48-
for (const bodyLine of codeBody) {
49-
yield {
50-
_insert: insertLineId,
51-
lines: {
52-
id: createNewLineId(userId),
53-
text: " " + bodyLine,
54-
},
55-
};
56-
}
31+
/** コードブロック本文のインデント数を計算する */
32+
export function countBodyIndent(
33+
codeBlock: Pick<TinyCodeBlock, "titleLine">,
34+
): number {
35+
return codeBlock.titleLine.text.length -
36+
codeBlock.titleLine.text.trimStart().length + 1;
5737
}

browser/websocket/updateCodeFile.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { pull } from "./pull.ts";
1010
import { getCodeBlocks, TinyCodeBlock } from "./getCodeBlocks.ts";
1111
import { createNewLineId, getUserId } from "./id.ts";
1212
import { diff, toExtendedChanges } from "../../deps/onp.ts";
13-
import { applyCommit } from "./_codeBlock.ts";
13+
import { applyCommit, countBodyIndent } from "./_codeBlock.ts";
1414

1515
/** コードブロックの上書きに使う情報のinterface */
1616
export interface CodeFile {
@@ -107,8 +107,7 @@ export const updateCodeFile = async (
107107
*/
108108
function flatCodeBodies(codeBlocks: readonly TinyCodeBlock[]): Line[] {
109109
return codeBlocks.map((block) => {
110-
const title = block.titleLine.text;
111-
const indent = title.length - title.trimStart().length + 1;
110+
const indent = countBodyIndent(block);
112111
return block.bodyLines.map((body) => {
113112
return { ...body, text: body.text.slice(indent) };
114113
});
@@ -131,9 +130,7 @@ function* makeCommits(
131130
},
132131
): Generator<DeleteCommit | InsertCommit | UpdateCommit, void, unknown> {
133132
function makeIndent(codeBlock: Pick<TinyCodeBlock, "titleLine">): string {
134-
const title = codeBlock.titleLine.text;
135-
const count = title.length - title.trimStart().length + 1;
136-
return " ".repeat(count);
133+
return " ".repeat(countBodyIndent(codeBlock));
137134
}
138135

139136
const codeBlocks: Pick<

0 commit comments

Comments
 (0)