1- import {
2- Change ,
3- CommitNotification ,
4- socketIO ,
5- wrap ,
6- } from "../../deps/socket.ts" ;
1+ import { CommitNotification , socketIO , wrap } from "../../deps/socket.ts" ;
72import { getProjectId , getUserId } from "./id.ts" ;
8- import { diffToChanges } from "./patch.ts" ;
93import { applyCommit } from "./applyCommit.ts" ;
4+ import { toTitleLc } from "../../title.ts" ;
5+ import { makeChanges } from "./makeChanges.ts" ;
106import type { Line } from "../../deps/scrapbox.ts" ;
117import { ensureEditablePage , pushCommit } from "./_fetch.ts" ;
128export type { CommitNotification } ;
@@ -48,9 +44,13 @@ export async function joinPageRoom(
4844 ] ) ;
4945
5046 // 接続したページの情報
51- let parentId = page . commitId ;
52- let created = page . persistent ;
53- let lines = page . lines ;
47+ let head = {
48+ persistent : page . persistent ,
49+ lines : page . lines ,
50+ image : page . image ,
51+ commitId : page . commitId ,
52+ linksLc : page . links . map ( ( link ) => toTitleLc ( link ) ) ,
53+ } ;
5454 const pageId = page . id ;
5555
5656 const io = await socketIO ( ) ;
@@ -63,55 +63,36 @@ export async function joinPageRoom(
6363 // subscribe the latest commit
6464 ( async ( ) => {
6565 for await ( const { id, changes } of response ( "commit" ) ) {
66- parentId = id ;
67- lines = applyCommit ( lines , changes , { updated : id , userId } ) ;
66+ head . commitId = id ;
67+ head . lines = applyCommit ( head . lines , changes , { updated : id , userId } ) ;
6868 }
6969 } ) ( ) ;
7070
7171 return {
7272 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-
11273 for ( let i = 0 ; i < 3 ; i ++ ) {
11374 try {
114- await tryPush ( ) ;
75+ const pending = update ( head . lines ) ;
76+ const newLines = pending instanceof Promise ? await pending : pending ;
77+ const changes = makeChanges ( head . lines , newLines , {
78+ userId,
79+ head,
80+ } ) ;
81+
82+ const { commitId } = await pushCommit ( request , changes , {
83+ parentId : head . commitId ,
84+ projectId,
85+ pageId,
86+ userId,
87+ } ) ;
88+
89+ // pushに成功したら、localにも変更を反映する
90+ head . commitId = commitId ;
91+ head . persistent = true ;
92+ head . lines = applyCommit ( head . lines , changes , {
93+ updated : commitId ,
94+ userId,
95+ } ) ;
11596 break ;
11697 } catch ( _e : unknown ) {
11798 if ( i === 2 ) {
@@ -122,9 +103,13 @@ export async function joinPageRoom(
122103 ) ;
123104 try {
124105 const page = await ensureEditablePage ( project , title ) ;
125- parentId = page . commitId ;
126- created = page . persistent ;
127- lines = page . lines ;
106+ head = {
107+ persistent : page . persistent ,
108+ lines : page . lines ,
109+ image : page . image ,
110+ commitId : page . commitId ,
111+ linksLc : page . links . map ( ( link ) => toTitleLc ( link ) ) ,
112+ } ;
128113 } catch ( e : unknown ) {
129114 throw e ;
130115 }
0 commit comments