From d0970c93420a7b50226b01fc5552276babd65638 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Sat, 1 Oct 2022 11:57:34 +0300 Subject: [PATCH] some fixes --- src/components/Editor/prosemirror/context.ts | 4 --- src/components/Editor/store/ctrl.ts | 34 ++---------------- src/components/Views/Article.tsx | 1 - src/components/Views/Home.tsx | 2 +- src/graphql/query/authors-all.ts | 4 +-- src/stores/zine/reactions.ts | 7 ++-- src/utils/p2p.ts | 36 ++++++++++++++++++++ 7 files changed, 45 insertions(+), 43 deletions(-) create mode 100644 src/utils/p2p.ts diff --git a/src/components/Editor/prosemirror/context.ts b/src/components/Editor/prosemirror/context.ts index c777b486..30bb814e 100644 --- a/src/components/Editor/prosemirror/context.ts +++ b/src/components/Editor/prosemirror/context.ts @@ -9,10 +9,6 @@ export const isMac = true export const mod = isMac ? 'Cmd' : 'Ctrl' export const alt = isMac ? 'Cmd' : 'Alt' -export const WEB_URL = - //'http://localhost:3000' - 'https://discoursio-editor-app.vercel.app' - export interface Args { cwd?: string file?: string diff --git a/src/components/Editor/store/ctrl.ts b/src/components/Editor/store/ctrl.ts index 20746048..32c2ef2a 100644 --- a/src/components/Editor/store/ctrl.ts +++ b/src/components/Editor/store/ctrl.ts @@ -3,10 +3,7 @@ import { v4 as uuidv4 } from 'uuid' import type { EditorState } from 'prosemirror-state' import { undo, redo } from 'prosemirror-history' import { selectAll, deleteSelection } from 'prosemirror-commands' -import * as Y from 'yjs' import { undo as yUndo, redo as yRedo } from 'y-prosemirror' -import { WebrtcProvider } from 'y-webrtc' -import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator' import { debounce } from 'ts-debounce' // import * as remote from '../prosemirror/remote' import { createSchema, createExtensions, createEmptyText } from '../prosemirror/setup' @@ -14,8 +11,8 @@ import { State, File, Config, ServiceError, newState } from '.' // import { isTauri, mod } from '../env' import { serialize, createMarkdownParser } from '../prosemirror/markdown' import { isEmpty, isInitialized } from '../prosemirror/state' -import { Awareness } from 'y-protocols/awareness' import { isServer } from 'solid-js/web' +import { roomConnect } from '../../../utils/p2p' const mod = 'Ctrl' const isTauri = false @@ -445,34 +442,7 @@ export const createCtrl = (initial: State): [Store, any] => { const backup = state.args?.room && state.collab?.room !== state.args.room const room = state.args?.room ?? uuidv4() - window.history.replaceState(null, '', `/${room}`) - - const ydoc = new Y.Doc() - const type = ydoc.getXmlFragment('prosemirror') - const webrtcOptions = { - awareness: new Awareness(ydoc), - filterBcConns: true, - maxConns: 33, - signaling: [ - // 'wss://signaling.discours.io', - // 'wss://stun.l.google.com:19302', - 'wss://y-webrtc-signaling-eu.herokuapp.com', - 'wss://signaling.yjs.dev' - ], - peerOpts: {}, - password: '' - } - const provider = new WebrtcProvider(room, ydoc, webrtcOptions) - const username = uniqueNamesGenerator({ - dictionaries: [adjectives, animals], - style: 'capital', - separator: ' ', - length: 2 - }) - - provider.awareness.setLocalStateField('user', { - name: username - }) + const [type, provider] = roomConnect(room) const extensions = createExtensions({ config: state.config, diff --git a/src/components/Views/Article.tsx b/src/components/Views/Article.tsx index 6c13a835..7be606eb 100644 --- a/src/components/Views/Article.tsx +++ b/src/components/Views/Article.tsx @@ -1,7 +1,6 @@ import { createEffect, createSignal, Show, Suspense } from 'solid-js' import { FullArticle } from '../Article/FullArticle' import { t } from '../../utils/intl' - import type { Shout } from '../../graphql/types.gen' import { loadArticleReactions, useReactionsStore } from '../../stores/zine/reactions' diff --git a/src/components/Views/Home.tsx b/src/components/Views/Home.tsx index 61274cf8..a2eb8854 100644 --- a/src/components/Views/Home.tsx +++ b/src/components/Views/Home.tsx @@ -86,7 +86,7 @@ export const HomeView = (props: HomeProps) => { } return ( - + diff --git a/src/graphql/query/authors-all.ts b/src/graphql/query/authors-all.ts index c1c4e00b..7afef282 100644 --- a/src/graphql/query/authors-all.ts +++ b/src/graphql/query/authors-all.ts @@ -1,8 +1,8 @@ import { gql } from '@urql/core' export default gql` - query AuthorssAllQuery($limit: Int!, $offset: Int!) { - authorsAll(limit: $limit, offset: $offset) { + query AuthorsAllQuery { + authorsAll { _id: slug slug name diff --git a/src/stores/zine/reactions.ts b/src/stores/zine/reactions.ts index daadb149..11f38204 100644 --- a/src/stores/zine/reactions.ts +++ b/src/stores/zine/reactions.ts @@ -3,7 +3,7 @@ import type { Reaction } from '../../graphql/types.gen' import { useStore } from '@nanostores/solid' import { apiClient } from '../../utils/apiClient' import { reduceBy } from '../../utils/reduce' - +// import { roomConnect } from '../../utils/p2p' // FIXME let reactionsOrdered: WritableAtom @@ -26,8 +26,9 @@ export const loadArticleReactions = async ({ limit?: number offset?: number }): Promise => { - const resp = await apiClient.getArticleReactions({ articleSlug, limit, offset }) - reactionsOrdered.set(resp) + const data = await apiClient.getArticleReactions({ articleSlug, limit, offset }) + // TODO: const [data, provider] = roomConnect(articleSlug) + reactionsOrdered.set(data) } export const loadReactions = async ({ diff --git a/src/utils/p2p.ts b/src/utils/p2p.ts new file mode 100644 index 00000000..977dd361 --- /dev/null +++ b/src/utils/p2p.ts @@ -0,0 +1,36 @@ +import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator' +import { Awareness } from 'y-protocols/awareness' +import { WebrtcProvider } from 'y-webrtc' +import * as Y from 'yjs' +import type { Reaction } from '../graphql/types.gen' + +export const roomConnect = (room, keyname = 'reactions'): [Reaction[], WebrtcProvider] => { + const ydoc = new Y.Doc() + const yarray = ydoc.getArray(keyname) + const webrtcOptions = { + awareness: new Awareness(ydoc), + filterBcConns: true, + maxConns: 33, + signaling: [ + // 'wss://signaling.discours.io', + // 'wss://stun.l.google.com:19302', + 'wss://y-webrtc-signaling-eu.herokuapp.com', + 'wss://signaling.yjs.dev' + ], + peerOpts: {}, + password: '' + } + const provider = new WebrtcProvider(room, ydoc, webrtcOptions) + const username = uniqueNamesGenerator({ + dictionaries: [adjectives, animals], + style: 'capital', + separator: ' ', + length: 2 + }) + + provider.awareness.setLocalStateField('user', { + name: username + }) + const data = yarray.toArray() as Reaction[] + return [data, provider] +}