|
| 1 | +import { Line } from "../../deps/scrapbox-rest.ts"; |
| 2 | +import { |
| 3 | + DeleteCommit, |
| 4 | + InsertCommit, |
| 5 | + Socket, |
| 6 | + socketIO, |
| 7 | + UpdateCommit, |
| 8 | +} from "../../deps/socket.ts"; |
| 9 | +import { TinyCodeBlock } from "../../rest/getCodeBlocks.ts"; |
| 10 | +import { diffToChanges } from "./diffToChanges.ts"; |
| 11 | +import { getUserId } from "./id.ts"; |
| 12 | +import { isSimpleCodeFile } from "./isSimpleCodeFile.ts"; |
| 13 | +import { pull } from "./pull.ts"; |
| 14 | +import { SimpleCodeFile } from "./updateCodeFile.ts"; |
| 15 | +import { |
| 16 | + applyCommit, |
| 17 | + countBodyIndent, |
| 18 | + extractFromCodeTitle, |
| 19 | +} from "./_codeBlock.ts"; |
| 20 | + |
| 21 | +export interface UpdateCodeBlockOptions { |
| 22 | + /** WebSocketの通信に使うsocket */ |
| 23 | + socket?: Socket; |
| 24 | + |
| 25 | + /** `true`でデバッグ出力ON */ |
| 26 | + debug?: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +/** コードブロックの中身を更新する |
| 30 | + * |
| 31 | + * newCodeにSimpleCodeFileオブジェクトを渡すと、そのオブジェクトに添ってコードブロックのファイル名も書き換えます |
| 32 | + * (文字列や文字列配列を渡した場合は書き換えません)。 |
| 33 | + * |
| 34 | + * @param newCode 更新後のコードブロック |
| 35 | + * @param target 更新対象のコードブロック |
| 36 | + * @param project 更新対象のコードブロックが存在するプロジェクト名 |
| 37 | + */ |
| 38 | +export const updateCodeBlock = async ( |
| 39 | + newCode: string | string[] | SimpleCodeFile, |
| 40 | + target: TinyCodeBlock, |
| 41 | + options?: UpdateCodeBlockOptions, |
| 42 | +) => { |
| 43 | + /** optionsの既定値はこの中に入れる */ |
| 44 | + const defaultOptions: Required<UpdateCodeBlockOptions> = { |
| 45 | + socket: options?.socket ?? await socketIO(), |
| 46 | + debug: false, |
| 47 | + }; |
| 48 | + const opt = options ? { ...defaultOptions, ...options } : defaultOptions; |
| 49 | + const { projectName, pageTitle } = target.pageInfo; |
| 50 | + const [ |
| 51 | + head, |
| 52 | + userId, |
| 53 | + ] = await Promise.all([ |
| 54 | + pull(projectName, pageTitle), |
| 55 | + getUserId(), |
| 56 | + ]); |
| 57 | + const newCodeBody = getCodeBody(newCode); |
| 58 | + const bodyIndent = countBodyIndent(target); |
| 59 | + const oldCodeWithoutIndent: Line[] = target.bodyLines.map((e) => { |
| 60 | + return { ...e, text: e.text.slice(bodyIndent) }; |
| 61 | + }); |
| 62 | + |
| 63 | + const diffGenerator = diffToChanges(oldCodeWithoutIndent, newCodeBody, { |
| 64 | + userId, |
| 65 | + }); |
| 66 | + const commits = [...fixCommits([...diffGenerator], target)]; |
| 67 | + if (isSimpleCodeFile(newCode)) { |
| 68 | + const titleCommit = makeTitleChangeCommit(newCode, target); |
| 69 | + if (titleCommit) commits.push(titleCommit); |
| 70 | + } |
| 71 | + |
| 72 | + if (opt.debug) { |
| 73 | + console.log("%cvvv original code block vvv", "color: limegreen;"); |
| 74 | + console.log(target); |
| 75 | + console.log("%cvvv new codes vvv", "color: limegreen;"); |
| 76 | + console.log(newCode); |
| 77 | + console.log("%cvvv commits vvv", "color: limegreen;"); |
| 78 | + console.log(commits); |
| 79 | + } |
| 80 | + |
| 81 | + await applyCommit(commits, head, projectName, pageTitle, opt.socket, userId); |
| 82 | + if (!options?.socket) opt.socket.disconnect(); |
| 83 | +}; |
| 84 | + |
| 85 | +/** コード本文のテキストを取得する */ |
| 86 | +const getCodeBody = (code: string | string[] | SimpleCodeFile): string[] => { |
| 87 | + const content = isSimpleCodeFile(code) ? code.content : code; |
| 88 | + if (Array.isArray(content)) return content; |
| 89 | + return content.split("\n"); |
| 90 | +}; |
| 91 | + |
| 92 | +/** insertコミットの行IDとtextのインデントを修正する */ |
| 93 | +function* fixCommits( |
| 94 | + commits: readonly (DeleteCommit | InsertCommit | UpdateCommit)[], |
| 95 | + target: TinyCodeBlock, |
| 96 | +): Generator<DeleteCommit | InsertCommit | UpdateCommit, void, unknown> { |
| 97 | + const { nextLine } = target; |
| 98 | + const indent = " ".repeat(countBodyIndent(target)); |
| 99 | + for (const commit of commits) { |
| 100 | + if ("_delete" in commit) { |
| 101 | + yield commit; |
| 102 | + } else if ( |
| 103 | + "_update" in commit |
| 104 | + ) { |
| 105 | + yield { |
| 106 | + ...commit, |
| 107 | + lines: { |
| 108 | + ...commit.lines, |
| 109 | + text: indent + commit.lines.text, |
| 110 | + }, |
| 111 | + }; |
| 112 | + } else if ( |
| 113 | + commit._insert != "_end" || |
| 114 | + nextLine === null |
| 115 | + ) { |
| 116 | + yield { |
| 117 | + ...commit, |
| 118 | + lines: { |
| 119 | + ...commit.lines, |
| 120 | + text: indent + commit.lines.text, |
| 121 | + }, |
| 122 | + }; |
| 123 | + } else { |
| 124 | + yield { |
| 125 | + _insert: nextLine.id, |
| 126 | + lines: { |
| 127 | + ...commit.lines, |
| 128 | + text: indent + commit.lines.text, |
| 129 | + }, |
| 130 | + }; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/** コードタイトルが違う場合は書き換える */ |
| 136 | +const makeTitleChangeCommit = ( |
| 137 | + code: SimpleCodeFile, |
| 138 | + target: Pick<TinyCodeBlock, "titleLine">, |
| 139 | +): UpdateCommit | null => { |
| 140 | + const lineId = target.titleLine.id; |
| 141 | + const targetTitle = extractFromCodeTitle(target.titleLine.text); |
| 142 | + if ( |
| 143 | + targetTitle && |
| 144 | + code.filename.trim() == targetTitle.filename && |
| 145 | + code.lang?.trim() == targetTitle.lang |
| 146 | + ) return null; |
| 147 | + const ext = (() => { |
| 148 | + const matched = code.filename.match(/.+\.(.*)$/); |
| 149 | + if (matched === null) return code.filename; |
| 150 | + else if (matched[1] === "") return ""; |
| 151 | + else return matched[1].trim(); |
| 152 | + })(); |
| 153 | + const title = code.filename + |
| 154 | + (code.lang && code.lang != ext ? `(${code.lang})` : ""); |
| 155 | + return { |
| 156 | + _update: lineId, |
| 157 | + lines: { |
| 158 | + text: " ".repeat(countBodyIndent(target) - 1) + "code:" + title, |
| 159 | + }, |
| 160 | + }; |
| 161 | +}; |
0 commit comments