UBERF-7931 Get rid of file collab provider (#6380)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov
2024-08-23 14:16:11 +07:00
committed by GitHub
parent 6f2c9ae9fe
commit 9b7880f86a
5 changed files with 51 additions and 78 deletions
@@ -43,7 +43,7 @@
import { deleteAttachment } from '../command/deleteAttachment'
import { textEditorCommandHandler } from '../commands'
import { EditorKitOptions, getEditorKit } from '../../src/kits/editor-kit'
import { DirectStorageProvider } from '../provider/storage'
import { IndexeddbProvider } from '../provider/indexeddb'
import { TiptapCollabProvider } from '../provider/tiptap'
import { formatCollaborativeDocumentId, formatPlatformDocumentId } from '../provider/utils'
import textEditor, {
@@ -121,7 +121,7 @@
const ydoc = getContext<YDoc>(CollaborationIds.Doc) ?? new YDoc()
const contextProvider = getContext<TiptapCollabProvider>(CollaborationIds.Provider)
const localProvider = contextProvider === undefined ? new DirectStorageProvider(collaborativeDoc, ydoc) : undefined
const localProvider = new IndexeddbProvider(collaborativeDoc, ydoc)
const remoteProvider: TiptapCollabProvider =
contextProvider ??
@@ -142,7 +142,7 @@
$: loading = !localSynced && !remoteSynced
$: editable = !readonly && remoteSynced
void localProvider?.loaded.then(() => (localSynced = true))
void localProvider.loaded.then(() => (localSynced = true))
void remoteProvider.loaded.then(() => (remoteSynced = true))
let editor: Editor
@@ -480,7 +480,7 @@
if (contextProvider === undefined) {
remoteProvider.destroy()
}
localProvider?.destroy()
void localProvider.destroy()
})
</script>
@@ -0,0 +1,32 @@
//
// Copyright © 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { collaborativeDocParse, type CollaborativeDoc } from '@hcengineering/core'
import { type Doc as YDoc } from 'yjs'
import { IndexeddbPersistence } from 'y-indexeddb'
export class IndexeddbProvider extends IndexeddbPersistence {
loaded: Promise<void>
constructor (collaborativeDoc: CollaborativeDoc, doc: YDoc) {
const { documentId, versionId } = collaborativeDocParse(collaborativeDoc)
const name = `${documentId}/${versionId}`
super(name, doc)
this.loaded = new Promise((resolve) => {
this.on('synced', resolve)
})
}
}
@@ -1,71 +0,0 @@
//
// Copyright © 2023, 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { collaborativeDocParse, type Blob, type CollaborativeDoc, type Ref } from '@hcengineering/core'
import { getFileUrl } from '@hcengineering/presentation'
import { ObservableV2 as Observable } from 'lib0/observable'
import { applyUpdate, type Doc as YDoc } from 'yjs'
interface EVENTS {
synced: (...args: any[]) => void
}
async function fetchContent (blob: Ref<Blob>, doc: YDoc): Promise<boolean> {
const update = await fetchBlobContent(blob)
if (update !== undefined) {
applyUpdate(doc, update)
return true
}
return false
}
async function fetchBlobContent (_id: Ref<Blob>): Promise<Uint8Array | undefined> {
try {
const href = getFileUrl(_id)
const res = await fetch(href)
if (res.ok) {
const blob = await res.blob()
const buffer = await blob.arrayBuffer()
return new Uint8Array(buffer)
}
} catch (err: any) {
console.error(err)
}
return undefined
}
export class DirectStorageProvider extends Observable<EVENTS> {
loaded: Promise<void>
constructor (collaborativeDoc: CollaborativeDoc, doc: YDoc) {
super()
this.loaded = new Promise((resolve) => {
this.on('synced', resolve)
})
const { documentId, versionId } = collaborativeDocParse(collaborativeDoc)
if (versionId === 'HEAD') {
void fetchContent(documentId as Ref<Blob>, doc).then((synced) => {
if (synced) {
this.emit('synced', [this])
}
})
}
}
}