fix(commons): extract name of markdown content yjs channel into the commons package

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-02-09 14:21:26 +01:00
parent 329a4d4e55
commit 11c2f57e4b
6 changed files with 22 additions and 13 deletions

View file

@ -3,7 +3,10 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { encodeDocumentUpdateMessage } from '@hedgedoc/commons'; import {
encodeDocumentUpdateMessage,
MARKDOWN_CONTENT_CHANNEL_NAME,
} from '@hedgedoc/commons';
import { Doc } from 'yjs'; import { Doc } from 'yjs';
import { RealtimeNote } from './realtime-note'; import { RealtimeNote } from './realtime-note';
@ -13,8 +16,6 @@ import { WebsocketConnection } from './websocket-connection';
* This is the implementation of {@link Doc YDoc} which includes additional handlers for message sending and receiving. * This is the implementation of {@link Doc YDoc} which includes additional handlers for message sending and receiving.
*/ */
export class WebsocketDoc extends Doc { export class WebsocketDoc extends Doc {
private static readonly channelName = 'markdownContent';
/** /**
* Creates a new WebsocketDoc instance. * Creates a new WebsocketDoc instance.
* *
@ -55,7 +56,7 @@ export class WebsocketDoc extends Doc {
* @private * @private
*/ */
private initializeContent(initialContent: string): void { private initializeContent(initialContent: string): void {
this.getText(WebsocketDoc.channelName).insert(0, initialContent); this.getText(MARKDOWN_CONTENT_CHANNEL_NAME).insert(0, initialContent);
} }
/** /**
@ -66,6 +67,6 @@ export class WebsocketDoc extends Doc {
* @return The current note content. * @return The current note content.
*/ */
public getCurrentContent(): string { public getCurrentContent(): string {
return this.getText(WebsocketDoc.channelName).toString(); return this.getText(MARKDOWN_CONTENT_CHANNEL_NAME).toString();
} }
} }

View file

@ -0,0 +1,7 @@
/*
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
export const MARKDOWN_CONTENT_CHANNEL_NAME = 'markdownContent'

View file

@ -33,3 +33,5 @@ export {
export type { MessageTransporterEvents } from './y-doc-message-transporter.js' export type { MessageTransporterEvents } from './y-doc-message-transporter.js'
export { waitForOtherPromisesToFinish } from './utils/wait-for-other-promises-to-finish.js' export { waitForOtherPromisesToFinish } from './utils/wait-for-other-promises-to-finish.js'
export { MARKDOWN_CONTENT_CHANNEL_NAME } from './constants/markdown-content-channel-name.js'

View file

@ -3,6 +3,7 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { MARKDOWN_CONTENT_CHANNEL_NAME } from './constants/markdown-content-channel-name.js'
import { encodeDocumentUpdateMessage } from './messages/document-update-message.js' import { encodeDocumentUpdateMessage } from './messages/document-update-message.js'
import { MessageType } from './messages/message-type.enum.js' import { MessageType } from './messages/message-type.enum.js'
import { YDocMessageTransporter } from './y-doc-message-transporter.js' import { YDocMessageTransporter } from './y-doc-message-transporter.js'
@ -55,9 +56,9 @@ describe('message transporter', () =>
const docClient2: Doc = new Doc() const docClient2: Doc = new Doc()
const dummyAwareness: Awareness = new Awareness(docServer) const dummyAwareness: Awareness = new Awareness(docServer)
const textServer = docServer.getText('markdownContent') const textServer = docServer.getText(MARKDOWN_CONTENT_CHANNEL_NAME)
const textClient1 = docClient1.getText('markdownContent') const textClient1 = docClient1.getText(MARKDOWN_CONTENT_CHANNEL_NAME)
const textClient2 = docClient2.getText('markdownContent') const textClient2 = docClient2.getText(MARKDOWN_CONTENT_CHANNEL_NAME)
textServer.insert(0, 'This is a test note') textServer.insert(0, 'This is a test note')
textServer.observe(() => textServer.observe(() =>

View file

@ -3,8 +3,7 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { MARKDOWN_CONTENT_CHANNEL_NAME } from './use-markdown-content-y-text' import { MARKDOWN_CONTENT_CHANNEL_NAME, YDocMessageTransporter } from '@hedgedoc/commons'
import { YDocMessageTransporter } from '@hedgedoc/commons'
import type { Awareness } from 'y-protocols/awareness' import type { Awareness } from 'y-protocols/awareness'
import type { Doc } from 'yjs' import type { Doc } from 'yjs'

View file

@ -3,11 +3,10 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { MARKDOWN_CONTENT_CHANNEL_NAME } from '@hedgedoc/commons'
import { useMemo } from 'react' import { useMemo } from 'react'
import type { Doc } from 'yjs' import type { Doc } from 'yjs'
import type { YText } from 'yjs/dist/src/types/YText' import type { Text as YText } from 'yjs'
export const MARKDOWN_CONTENT_CHANNEL_NAME = 'markdownContent'
/** /**
* Extracts the y-text channel that saves the markdown content from the given yDoc. * Extracts the y-text channel that saves the markdown content from the given yDoc.