• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EcrituresNumeriques / stylo / 14840812320

05 May 2025 04:02PM UTC coverage: 37.726% (-0.02%) from 37.741%
14840812320

push

github

ggrossetie
chore: add console.debug to troubleshoot desync

532 of 760 branches covered (70.0%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 1 file covered. (0.0%)

5239 of 14537 relevant lines covered (36.04%)

2.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/front/src/components/collaborative/CollaborativeTextEditor.jsx
1
import Editor from '@monaco-editor/react'
×
2
import clsx from 'clsx'
×
3
import throttle from 'lodash.throttle'
×
4
import React, { useCallback, useEffect, useMemo, useRef } from 'react'
×
5
import { shallowEqual, useDispatch, useSelector } from 'react-redux'
×
6
import { MonacoBinding } from 'y-monaco'
×
7
import { useArticleVersion, useEditableArticle } from '../../hooks/article.js'
×
8
import { useBibliographyCompletion } from '../../hooks/bibliography.js'
×
9
import { useCollaboration } from '../../hooks/collaboration.js'
×
10
import Alert from '../molecules/Alert.jsx'
×
11

12
import Loading from '../molecules/Loading.jsx'
×
13
import defaultEditorOptions from '../Write/providers/monaco/options.js'
×
14
import CollaborativeEditorStatus from './CollaborativeEditorStatus.jsx'
×
15
import CollaborativeEditorWebSocketStatus from './CollaborativeEditorWebSocketStatus.jsx'
×
16

17
import styles from './CollaborativeTextEditor.module.scss'
×
18
import MonacoEditor from '../molecules/MonacoEditor.jsx'
×
19

20
/**
21
 * @param {object} props
22
 * @param {string} props.articleId
23
 * @param {string|undefined} props.versionId
24
 * @returns {Element}
25
 */
26
export default function CollaborativeTextEditor({ articleId, versionId }) {
×
27
  const { yText, awareness, websocketStatus, dynamicStyles } = useCollaboration(
×
28
    { articleId, versionId }
×
29
  )
×
30
  const { version, error, isLoading } = useArticleVersion({ versionId })
×
31
  const { provider: bibliographyCompletionProvider } =
×
32
    useBibliographyCompletion()
×
33
  const { bibliography } = useEditableArticle({
×
34
    articleId,
×
35
    versionId,
×
36
  })
×
37
  const dispatch = useDispatch()
×
38
  const editorRef = useRef(null)
×
39
  const editorCursorPosition = useSelector(
×
40
    (state) => state.editorCursorPosition,
×
41
    shallowEqual
×
42
  )
×
43

44
  const hasVersion = useMemo(() => !!versionId, [versionId])
×
45

46
  const options = useMemo(
×
47
    () => ({
×
48
      ...defaultEditorOptions,
×
49
      contextmenu: hasVersion ? false : websocketStatus === 'connected',
×
50
      readOnly: hasVersion ? true : websocketStatus !== 'connected',
×
51
    }),
×
52
    [websocketStatus, hasVersion]
×
53
  )
×
54

55
  const handleUpdateArticleStructureAndStats = throttle(
×
56
    ({ text }) => {
×
57
      dispatch({ type: 'UPDATE_ARTICLE_STATS', md: text })
×
58
      dispatch({ type: 'UPDATE_ARTICLE_STRUCTURE', md: text })
×
59
    },
×
60
    250,
×
61
    { leading: false, trailing: true }
×
62
  )
×
63

64
  const handleCollaborativeEditorDidMount = useCallback(
×
65
    (editor, monaco) => {
×
66
      editorRef.current = editor
×
67
      const completionProvider = bibliographyCompletionProvider.register(monaco)
×
68
      editor.onDidDispose(() => completionProvider.dispose())
×
69
      if (yText && awareness) {
×
70
        new MonacoBinding(
×
71
          yText,
×
72
          editor.getModel(),
×
73
          new Set([editor]),
×
74
          awareness
×
75
        )
×
76
      }
×
77
    },
×
78
    [yText, awareness]
×
79
  )
×
80

81
  const handleEditorDidMount = useCallback((editor) => {
×
82
    editorRef.current = editor
×
83
  }, [])
×
84

85
  let timeoutId
×
86
  useEffect(() => {
×
87
    if (yText) {
×
NEW
88
      yText.observe(function (yTextEvent, transaction) {
×
NEW
89
        console.debug('yText.observe', {
×
NEW
90
          yTextEvent,
×
NEW
91
          transaction,
×
NEW
92
          text: yText.toString(),
×
NEW
93
          delta: yText.toDelta(),
×
NEW
94
        })
×
95
        dispatch({
×
96
          type: 'UPDATE_ARTICLE_WORKING_COPY_STATUS',
×
97
          status: 'syncing',
×
98
        })
×
99
        if (timeoutId) {
×
100
          clearTimeout(timeoutId)
×
101
        }
×
102
        timeoutId = setTimeout(() => {
×
103
          dispatch({
×
104
            type: 'UPDATE_ARTICLE_WORKING_COPY_STATUS',
×
105
            status: 'synced',
×
106
          })
×
107
        }, 4000)
×
108

109
        handleUpdateArticleStructureAndStats({ text: yText.toString() })
×
110
      })
×
111
    }
×
112
  }, [yText])
×
113

114
  useEffect(() => {
×
115
    if (version) {
×
116
      dispatch({ type: 'UPDATE_ARTICLE_STATS', md: version.md })
×
117
      dispatch({ type: 'UPDATE_ARTICLE_STRUCTURE', md: version.md })
×
118
    }
×
119
  }, [version])
×
120

121
  useEffect(() => {
×
122
    if (bibliography) {
×
123
      bibliographyCompletionProvider.bibTeXEntries = bibliography.entries
×
124
    }
×
125
  }, [bibliography])
×
126

127
  useEffect(() => {
×
128
    const line = editorCursorPosition.lineNumber
×
129
    const editor = editorRef.current
×
130
    editor?.focus()
×
131
    const endOfLineColumn = editor?.getModel()?.getLineMaxColumn(line + 1)
×
132
    editor?.setPosition({ lineNumber: line + 1, column: endOfLineColumn })
×
133
    editor?.revealLineNearTop(line + 1, 1) // smooth
×
134
  }, [editorRef, editorCursorPosition])
×
135

136
  if (!yText && !version) {
×
137
    return <Loading />
×
138
  }
×
139

140
  if (isLoading) {
×
141
    return <Loading />
×
142
  }
×
143

144
  if (error) {
×
145
    return <Alert message={error.message} />
×
146
  }
×
147

148
  return (
×
149
    <>
×
150
      <style>{dynamicStyles}</style>
×
151
      <CollaborativeEditorStatus versionId={versionId} />
×
152
      <div className={styles.inlineStatus}>
×
153
        <CollaborativeEditorWebSocketStatus status={websocketStatus} />
×
154
      </div>
×
155
      {version && (
×
156
        <MonacoEditor
×
157
          width={'100%'}
×
158
          height={'auto'}
×
159
          value={version.md}
×
160
          options={options}
×
161
          className={styles.editor}
×
162
          defaultLanguage="markdown"
×
163
          onMount={handleEditorDidMount}
×
164
        />
×
165
      )}
166
      <div
×
167
        className={clsx(styles.collaborativeEditor, versionId && styles.hidden)}
×
168
      >
169
        <MonacoEditor
×
170
          width={'100%'}
×
171
          height={'auto'}
×
172
          options={options}
×
173
          className={styles.editor}
×
174
          defaultLanguage="markdown"
×
175
          onMount={handleCollaborativeEditorDidMount}
×
176
        />
×
177
      </div>
×
178
    </>
×
179
  )
180
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc