55 wrap ,
66} from "../../deps/socket.ts" ;
77import { getProjectId , getUserId } from "./id.ts" ;
8- import { diffToChanges } from "./patch.ts" ;
98import { applyCommit } from "./applyCommit.ts" ;
9+ import { toTitleLc } from "../../title.ts" ;
10+ import { makeChanges } from "./makeChanges.ts" ;
1011import type { Line } from "../../deps/scrapbox.ts" ;
1112import { ensureEditablePage , pushCommit } from "./_fetch.ts" ;
1213export type { CommitNotification } ;
@@ -48,9 +49,13 @@ export async function joinPageRoom(
4849 ] ) ;
4950
5051 // 接続したページの情報
51- let parentId = page . commitId ;
52- let created = page . persistent ;
53- let lines = page . lines ;
52+ let head = {
53+ persistent : page . persistent ,
54+ lines : page . lines ,
55+ image : page . image ,
56+ commitId : page . commitId ,
57+ linksLc : page . links . map ( ( link ) => toTitleLc ( link ) ) ,
58+ } ;
5459 const pageId = page . id ;
5560
5661 const io = await socketIO ( ) ;
@@ -63,55 +68,36 @@ export async function joinPageRoom(
6368 // subscribe the latest commit
6469 ( async ( ) => {
6570 for await ( const { id, changes } of response ( "commit" ) ) {
66- parentId = id ;
67- lines = applyCommit ( lines , changes , { updated : id , userId } ) ;
71+ head . commitId = id ;
72+ head . lines = applyCommit ( head . lines , changes , { updated : id , userId } ) ;
6873 }
6974 } ) ( ) ;
7075
7176 return {
7277 patch : async ( update : ( before : Line [ ] ) => string [ ] | Promise < string [ ] > ) => {
73- const tryPush = async ( ) => {
74- const pending = update ( lines ) ;
75- const newLines = pending instanceof Promise ? await pending : pending ;
76- const changes : Change [ ] = [
77- ...diffToChanges ( lines , newLines , { userId } ) ,
78- ] ;
79-
80- // 変更後のlinesを計算する
81- const changedLines = applyCommit ( lines , changes , {
82- userId,
83- } ) ;
84- // タイトルの変更チェック
85- // 空ページの場合もタイトル変更commitを入れる
86- if ( lines [ 0 ] . text !== changedLines [ 0 ] . text || ! created ) {
87- changes . push ( { title : changedLines [ 0 ] . text } ) ;
88- }
89- // サムネイルの変更チェック
90- const oldDescriptions = lines . slice ( 1 , 6 ) . map ( ( line ) => line . text ) ;
91- const newDescriptions = changedLines . slice ( 1 , 6 ) . map ( ( lines ) =>
92- lines . text
93- ) ;
94- if ( oldDescriptions . join ( "\n" ) !== newDescriptions . join ( "\n" ) ) {
95- changes . push ( { descriptions : newDescriptions } ) ;
96- }
97-
98- // pushする
99- const { commitId } = await pushCommit ( request , changes , {
100- parentId,
101- projectId,
102- pageId,
103- userId,
104- } ) ;
105-
106- // pushに成功したら、localにも変更を反映する
107- parentId = commitId ;
108- created = true ;
109- lines = changedLines ;
110- } ;
111-
11278 for ( let i = 0 ; i < 3 ; i ++ ) {
11379 try {
114- await tryPush ( ) ;
80+ const pending = update ( head . lines ) ;
81+ const newLines = pending instanceof Promise ? await pending : pending ;
82+ const changes = makeChanges ( head . lines , newLines , {
83+ userId,
84+ head,
85+ } ) ;
86+
87+ const { commitId } = await pushCommit ( request , changes , {
88+ parentId : head . commitId ,
89+ projectId,
90+ pageId,
91+ userId,
92+ } ) ;
93+
94+ // pushに成功したら、localにも変更を反映する
95+ head . commitId = commitId ;
96+ head . persistent = true ;
97+ head . lines = applyCommit ( head . lines , changes , {
98+ updated : commitId ,
99+ userId,
100+ } ) ;
115101 break ;
116102 } catch ( _e : unknown ) {
117103 if ( i === 2 ) {
@@ -122,9 +108,13 @@ export async function joinPageRoom(
122108 ) ;
123109 try {
124110 const page = await ensureEditablePage ( project , title ) ;
125- parentId = page . commitId ;
126- created = page . persistent ;
127- lines = page . lines ;
111+ head = {
112+ persistent : page . persistent ,
113+ lines : page . lines ,
114+ image : page . image ,
115+ commitId : page . commitId ,
116+ linksLc : page . links . map ( ( link ) => toTitleLc ( link ) ) ,
117+ } ;
128118 } catch ( e : unknown ) {
129119 throw e ;
130120 }
0 commit comments