1
- import {
2
- Change ,
3
- CommitNotification ,
4
- socketIO ,
5
- wrap ,
6
- } from "../../deps/socket.ts" ;
1
+ import { CommitNotification , socketIO , wrap } from "../../deps/socket.ts" ;
7
2
import { getProjectId , getUserId } from "./id.ts" ;
8
- import { diffToChanges } from "./patch.ts" ;
9
3
import { applyCommit } from "./applyCommit.ts" ;
4
+ import { toTitleLc } from "../../title.ts" ;
5
+ import { makeChanges } from "./makeChanges.ts" ;
10
6
import type { Line } from "../../deps/scrapbox.ts" ;
11
7
import { ensureEditablePage , pushCommit } from "./_fetch.ts" ;
12
8
export type { CommitNotification } ;
@@ -48,9 +44,13 @@ export async function joinPageRoom(
48
44
] ) ;
49
45
50
46
// 接続したページの情報
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
+ } ;
54
54
const pageId = page . id ;
55
55
56
56
const io = await socketIO ( ) ;
@@ -63,55 +63,36 @@ export async function joinPageRoom(
63
63
// subscribe the latest commit
64
64
( async ( ) => {
65
65
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 } ) ;
68
68
}
69
69
} ) ( ) ;
70
70
71
71
return {
72
72
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
-
112
73
for ( let i = 0 ; i < 3 ; i ++ ) {
113
74
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
+ } ) ;
115
96
break ;
116
97
} catch ( _e : unknown ) {
117
98
if ( i === 2 ) {
@@ -122,9 +103,13 @@ export async function joinPageRoom(
122
103
) ;
123
104
try {
124
105
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
+ } ;
128
113
} catch ( e : unknown ) {
129
114
throw e ;
130
115
}
0 commit comments