Skip to content

Commit 59b88ff

Browse files
committed
既存のコードブロックが無かった際に使用するコードを最小限にした
1 parent e188921 commit 59b88ff

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

browser/websocket/updateCodeFile.ts

Lines changed: 50 additions & 34 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, makeCommitsNewCodeBlock } from "./_codeBlock.ts";
13+
import { applyCommit } from "./_codeBlock.ts";
1414

1515
/** コードブロックの上書きに使う情報のinterface */
1616
export interface CodeFile {
@@ -117,7 +117,7 @@ function flatCodeBodies(codeBlocks: readonly TinyCodeBlock[]): Line[] {
117117

118118
/** コードブロックの差分からコミットデータを作成する */
119119
function* makeCommits(
120-
codeBlocks: TinyCodeBlock[],
120+
_codeBlocks: readonly TinyCodeBlock[],
121121
codeFile: CodeFile,
122122
lines: Line[],
123123
{ userId, insertPositionIfNotExist, isInsertEmptyLineInTail }: {
@@ -130,40 +130,38 @@ function* makeCommits(
130130
>;
131131
},
132132
): Generator<DeleteCommit | InsertCommit | UpdateCommit, void, unknown> {
133-
function makeIndent(codeBlock: TinyCodeBlock): string {
133+
function makeIndent(codeBlock: Pick<TinyCodeBlock, "titleLine">): string {
134134
const title = codeBlock.titleLine.text;
135135
const count = title.length - title.trimStart().length + 1;
136136
return " ".repeat(count);
137137
}
138138

139-
const codeBodies = flatCodeBodies(codeBlocks);
140-
if (codeBodies.length <= 0) {
141-
// ページ内にコードブロックが無かった場合は新しく作成して終了する
139+
const codeBlocks: Pick<
140+
TinyCodeBlock,
141+
"titleLine" | "bodyLines" | "nextLine"
142+
>[] = [..._codeBlocks];
143+
const codeBodies = flatCodeBodies(_codeBlocks);
144+
if (codeBlocks.length <= 0) {
145+
// ページ内にコードブロックが無かった場合は新しく作成
142146
if (insertPositionIfNotExist === "notInsert") return;
143-
const insertLineId = insertPositionIfNotExist === "top" && lines.length > 1
144-
? lines[1].id
145-
: "_end";
146-
const commits = makeCommitsNewCodeBlock(
147-
codeFile,
148-
insertLineId,
149-
{ userId },
150-
);
151-
let isInsertBottom = false;
152-
for (const commit of commits) {
153-
if (commit._insert == "_end") isInsertBottom = true;
154-
yield commit;
155-
}
156-
if (isInsertBottom && isInsertEmptyLineInTail) {
157-
// 空行承り太郎
158-
yield {
159-
_insert: "_end",
160-
lines: {
161-
id: createNewLineId(userId),
162-
text: "",
163-
},
164-
};
165-
}
166-
return;
147+
const nextLine = insertPositionIfNotExist === "top" && lines.length > 1
148+
? lines[1]
149+
: null;
150+
const title = {
151+
// コードブロックのタイトル行
152+
_insert: nextLine?.id ?? "_end",
153+
lines: {
154+
id: createNewLineId(userId),
155+
text: makeCodeBlockTitle(codeFile),
156+
},
157+
};
158+
yield title;
159+
// 新しく作成したコードブロックの情報を追記
160+
codeBlocks.push({
161+
titleLine: { ...title.lines, userId, created: -1, updated: -1 },
162+
bodyLines: [],
163+
nextLine: nextLine,
164+
});
167165
}
168166

169167
// 差分を求める
@@ -174,6 +172,7 @@ function* makeCommits(
174172
: codeFile.content.split("\n"),
175173
);
176174
let lineNo = 0;
175+
let isInsertBottom = false;
177176
for (const change of toExtendedChanges(buildSES())) {
178177
// 差分からcommitを作成
179178
const { lineId, codeIndex } =
@@ -194,17 +193,19 @@ function* makeCommits(
194193
})();
195194
const codeBlock = codeBlocks[codeIndex];
196195
if (change.type == "added") {
197-
const codeBlockInsert =
198-
lineId == codeBlock.bodyLines[0].id && codeIndex >= 1
196+
const insertCodeBlock =
197+
lineId == codeBlock.bodyLines[0]?.id && codeIndex >= 1
199198
? codeBlocks[codeIndex - 1]
200199
: codeBlocks[codeIndex];
200+
const id = insertCodeBlock?.nextLine?.id ?? "_end";
201201
yield {
202-
_insert: codeBlockInsert.nextLine?.id ?? "_end",
202+
_insert: id,
203203
lines: {
204204
id: createNewLineId(userId),
205-
text: makeIndent(codeBlockInsert) + change.value,
205+
text: makeIndent(insertCodeBlock) + change.value,
206206
},
207207
};
208+
if (id == "_end") isInsertBottom = true;
208209
continue;
209210
} else if (change.type == "deleted") {
210211
yield {
@@ -221,4 +222,19 @@ function* makeCommits(
221222
}
222223
lineNo++;
223224
}
225+
if (isInsertBottom && isInsertEmptyLineInTail) {
226+
// 空行承り太郎
227+
yield {
228+
_insert: "_end",
229+
lines: {
230+
id: createNewLineId(userId),
231+
text: "",
232+
},
233+
};
234+
}
235+
}
236+
237+
function makeCodeBlockTitle(code: CodeFile) {
238+
const codeName = code.filename + (code.lang ? `(${code.lang})` : "");
239+
return `code:${codeName}`;
224240
}

0 commit comments

Comments
 (0)