File size: 6,823 Bytes
c1f12bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d7bbe
083ce88
 
c1f12bf
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d7bbe
 
 
 
 
 
316ef9c
c1f12bf
 
 
d0d7bbe
 
c1f12bf
 
 
316ef9c
 
 
 
 
d0d7bbe
 
 
 
 
 
 
 
 
 
 
c1f12bf
5eac338
c1f12bf
d0d7bbe
 
 
 
c1f12bf
d0d7bbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eac338
 
 
 
c1f12bf
 
 
5eac338
316ef9c
c1f12bf
5eac338
c1f12bf
 
 
 
5eac338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d7bbe
 
c1f12bf
 
 
d0d7bbe
c1f12bf
d0d7bbe
 
 
 
 
 
c1f12bf
 
 
d0d7bbe
 
 
 
 
 
316ef9c
 
 
c1f12bf
316ef9c
c1f12bf
 
 
316ef9c
c1f12bf
 
 
316ef9c
 
c1f12bf
316ef9c
 
 
c1f12bf
316ef9c
 
 
 
c1f12bf
 
 
 
 
316ef9c
 
 
c1f12bf
316ef9c
 
 
 
c1f12bf
 
 
 
 
316ef9c
 
c1f12bf
316ef9c
c1f12bf
 
 
316ef9c
 
 
 
 
 
c1f12bf
316ef9c
 
 
 
 
 
 
c1f12bf
316ef9c
 
 
 
 
 
 
 
 
 
c1f12bf
316ef9c
 
 
 
 
 
 
 
 
 
 
c1f12bf
 
316ef9c
 
 
 
083ce88
c1f12bf
 
 
083ce88
 
d0d7bbe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
'use client'

import { create } from 'zustand'
import { Monaco } from '@monaco-editor/react'
import MonacoEditor from 'monaco-editor'
import { ClapProject, ClapSegmentCategory } from '@aitube/clap'
import {
  TimelineStore,
  useTimeline,
  leftBarTrackScaleWidth,
} from '@aitube/timeline'
import {
  ScriptEditorStore,
  EditorView,
  ScrollData,
} from '@aitube/clapper-services'

import { getDefaultScriptEditorState } from './getDefaultScriptEditorState'

export const useScriptEditor = create<ScriptEditorStore>((set, get) => ({
  ...getDefaultScriptEditorState(),
  setMonaco: (monaco?: Monaco) => {
    set({ monaco })
  },
  setTextModel: (textModel?: MonacoEditor.editor.ITextModel) => {
    set({ textModel })
  },
  setStandaloneCodeEditor: (
    standaloneCodeEditor?: MonacoEditor.editor.IStandaloneCodeEditor
  ) => {
    set({ standaloneCodeEditor })
  },
  setMouseIsInside: (mouseIsInside: boolean) => {
    set({ mouseIsInside })
  },
  loadDraftFromClap: (clap: ClapProject) => {
    const { setDraft } = get()

    setDraft(clap.meta.screenplay)
  },
  setDraft: (draft: string) => {
    const { draft: previousDraft, highlightElements, textModel } = get()
    if (draft === previousDraft) {
      return
    }
    set({ draft })

    if (!textModel) {
      return
    }
    // we need to update the model
    textModel?.setValue(draft)

    // and highlight the text again
    highlightElements()
  },
  publishDraftToTimeline: async (): Promise<void> => {
    const { draft } = get()
    console.log(`user asked to update the whole scene! this is expensive..`)
    // we can do something smart, which is to only reconstruct the impacted segments
    // and shift the rest along the time axis, without modifying it
  },
  onDidScrollChange: ({
    scrollHeight,
    scrollLeft,
    scrollTop,
    scrollWidth,
  }: ScrollData) => {
    const {
      scrollHeight: previousScrollHeight,
      scrollLeft: previousScrollLeft,
      scrollTop: previousScrollTop,
      scrollWidth: previousScrollWidth,
      mouseIsInside,
    } = get()

    // skip if nothing changed
    if (
      scrollHeight === previousScrollHeight &&
      scrollLeft === previousScrollLeft &&
      scrollTop === previousScrollTop &&
      scrollWidth === previousScrollWidth
    ) {
      return
    }

    set({
      scrollHeight,
      scrollLeft,
      scrollTop,
      scrollWidth,
    })

    // if the scroll event happened while we where inside the editor,
    // then we need to dispatch the it
    if (mouseIsInside) {
      const timeline: TimelineStore = useTimeline.getState()
      if (!timeline.timelineCamera || !timeline.timelineControls) {
        return
      }

      const { standaloneCodeEditor } = get()

      const scrollRatio = scrollTop / scrollHeight
      const scrollX = Math.round(
        leftBarTrackScaleWidth + scrollRatio * timeline.contentWidth
      )

      /*console.log({
        scrollHeight,
        scrollLeft,
        scrollTop,
        scrollWidth,
        scrollRatio,
        scrollX
      })
       */

      if (useTimeline.getState().scrolX !== scrollX) {
        useTimeline.setState({ scrollX })
        timeline.timelineCamera.position.setX(scrollX)
        timeline.timelineControls.target.setX(scrollX)
      }
    }
  },
  jumpCursorOnLineClick: (line?: number) => {
    if (typeof line !== 'number') {
      return
    }
    const timeline: TimelineStore = useTimeline.getState()

    const { lineNumberToMentionedSegments } = timeline

    const mentionedSegments = lineNumberToMentionedSegments[line] || []

    const firstMentionedSegment = mentionedSegments.at(0)

    if (typeof firstMentionedSegment?.startTimeInMs !== 'number') {
      return
    }

    const { startTimeInMs } = firstMentionedSegment

    timeline.setCursorTimestampAtInMs(startTimeInMs)
  },

  highlightElements: () => {
    const timeline: TimelineStore = useTimeline.getState()
    const { clap } = timeline

    const { textModel, standaloneCodeEditor, applyClassNameToKeywords } = get()
    if (!textModel || !standaloneCodeEditor || !clap) {
      return
    }

    const characters = clap.entities
      .filter((entity) => entity.category === ClapSegmentCategory.CHARACTER)
      .map((entity) => entity.triggerName)

    // any character
    applyClassNameToKeywords('entity entity-character', characters)

    // UPPERCASE CHARACTER
    applyClassNameToKeywords(
      'entity entity-character entity-highlight',
      characters,
      true
    )

    const locations = clap.entities
      .filter((entity) => entity.category === ClapSegmentCategory.LOCATION)
      .map((entity) => entity.triggerName)
    // any location
    applyClassNameToKeywords('entity entity-location', locations)

    // UPPERCASE LOCATION
    applyClassNameToKeywords(
      'entity entity-location entity-highlight',
      locations,
      true
    )
  },
  applyClassNameToKeywords: (
    className: string = '',
    keywords: string[] = [],
    caseSensitive = false
  ) => {
    const timeline: TimelineStore = useTimeline.getState()
    const { clap } = timeline

    const { textModel, standaloneCodeEditor } = get()
    if (!textModel || !standaloneCodeEditor || !clap) {
      return
    }

    keywords.forEach((entityTriggerName: string): void => {
      const matches: MonacoEditor.editor.FindMatch[] = textModel.findMatches(
        // searchString β€” The string used to search. If it is a regular expression, set isRegex to true.
        // searchString: string,
        entityTriggerName,

        // @param searchOnlyEditableRange β€” Limit the searching to only search inside the editable range of the model.
        // searchOnlyEditableRange: boolean,
        false,

        // / @param isRegex β€” Used to indicate that searchString is a regular expression.
        // isRegex: boolean,
        false,

        // @param matchCase β€” Force the matching to match lower/upper case exactly.
        // matchCase: boolean,
        caseSensitive,

        // @param wordSeparators β€” Force the matching to match entire words only. Pass null otherwise.
        // wordSeparators: string | null,
        null,

        // @param captureMatches β€” The result will contain the captured groups.
        // captureMatches: boolean,
        false

        // limitResultCount β€” Limit the number of results
        // limitResultCount?: number
      )

      matches.forEach((match: MonacoEditor.editor.FindMatch): void => {
        standaloneCodeEditor.createDecorationsCollection([
          {
            range: match.range,
            options: {
              isWholeLine: false,
              inlineClassName: className,
            },
          },
        ])
      })
    })
  },
  setCurrent: (current?: string) => {
    set({ current })
  },
  undo: () => {},
  redo: () => {},
}))