vzcode 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/package.json +3 -2
  2. package/src/cli.ts +208 -0
  3. package/src/client/AIAssist/AIAssistCodeMirrorKeyMap.ts +42 -0
  4. package/src/client/AIAssist/AIAssistWidget/index.tsx +135 -0
  5. package/src/client/AIAssist/AIAssistWidget/style.scss +47 -0
  6. package/src/client/AIAssist/Spinner.jsx +76 -0
  7. package/src/client/AIAssist/formatFile.ts +27 -0
  8. package/src/client/AIAssist/generateFilesContext.ts +25 -0
  9. package/src/client/AIAssist/index.ts +2 -0
  10. package/src/client/AIAssist/startAIAssist.ts +95 -0
  11. package/src/client/App/index.tsx +152 -0
  12. package/src/client/App/style.scss +82 -0
  13. package/src/client/App/usePending.ts +51 -0
  14. package/src/client/App/useShareDB.ts +161 -0
  15. package/src/client/CodeEditor/Copilot/index.ts +74 -0
  16. package/src/client/CodeEditor/InteractiveWidgets/boolToggler.ts +12 -0
  17. package/src/client/CodeEditor/InteractiveWidgets/colorPicker.ts +68 -0
  18. package/src/client/CodeEditor/InteractiveWidgets/colorsInTextPlugin.ts +174 -0
  19. package/src/client/CodeEditor/InteractiveWidgets/highlightWidgets.ts +79 -0
  20. package/src/client/CodeEditor/InteractiveWidgets/index.ts +44 -0
  21. package/src/client/CodeEditor/InteractiveWidgets/markdownCheckboxToggler.ts +12 -0
  22. package/src/client/CodeEditor/InteractiveWidgets/numberDragger.ts +20 -0
  23. package/src/client/CodeEditor/InteractiveWidgets/rotateWidget.ts +143 -0
  24. package/src/client/CodeEditor/InteractiveWidgets/urlClicker.test.ts +48 -0
  25. package/src/client/CodeEditor/InteractiveWidgets/urlClicker.ts +12 -0
  26. package/src/client/CodeEditor/InteractiveWidgets/vec2Slider.ts +25 -0
  27. package/src/client/CodeEditor/basicSetup.ts +73 -0
  28. package/src/client/CodeEditor/getOrCreateEditor.tsx +615 -0
  29. package/src/client/CodeEditor/index.tsx +202 -0
  30. package/src/client/CodeEditor/json1PresenceBroadcast.ts +41 -0
  31. package/src/client/CodeEditor/json1PresenceDisplay.ts +350 -0
  32. package/src/client/CodeEditor/rainbowBrackets.ts +122 -0
  33. package/src/client/CodeEditor/style.scss +156 -0
  34. package/src/client/CodeEditor/typescriptExtension/worker.ts +32 -0
  35. package/src/client/CodeErrorOverlay/index.tsx +77 -0
  36. package/src/client/CodeErrorOverlay/style.scss +33 -0
  37. package/src/client/Fonts/fonts.ts +8 -0
  38. package/src/client/Icons/AdjustmentSVG.tsx +19 -0
  39. package/src/client/Icons/BugSVG.tsx +20 -0
  40. package/src/client/Icons/CloseSVG.tsx +19 -0
  41. package/src/client/Icons/CssSVG.tsx +16 -0
  42. package/src/client/Icons/DirectoryArrowSVG.tsx +19 -0
  43. package/src/client/Icons/EditSVG.tsx +19 -0
  44. package/src/client/Icons/FileSVG.tsx +19 -0
  45. package/src/client/Icons/FolderSVG.tsx +20 -0
  46. package/src/client/Icons/GearSVG.tsx +16 -0
  47. package/src/client/Icons/HtmlSVG.tsx +16 -0
  48. package/src/client/Icons/JavaScriptSVG.tsx +16 -0
  49. package/src/client/Icons/JsonSVG.tsx +16 -0
  50. package/src/client/Icons/MarkdownSVG.tsx +16 -0
  51. package/src/client/Icons/MicOffSVG.tsx +35 -0
  52. package/src/client/Icons/MicSVG.tsx +25 -0
  53. package/src/client/Icons/NewSVG.tsx +19 -0
  54. package/src/client/Icons/PinSVG.tsx +14 -0
  55. package/src/client/Icons/PlaySVG.tsx +16 -0
  56. package/src/client/Icons/QuestionMarkSVG.tsx +18 -0
  57. package/src/client/Icons/ReactSVG.tsx +16 -0
  58. package/src/client/Icons/SearchSVG.tsx +20 -0
  59. package/src/client/Icons/SparklesSVG.tsx +23 -0
  60. package/src/client/Icons/SplitEditorSVG.tsx +34 -0
  61. package/src/client/Icons/StopSVG.tsx +15 -0
  62. package/src/client/Icons/SvelteSVG.tsx +16 -0
  63. package/src/client/Icons/TrashSVG.tsx +19 -0
  64. package/src/client/Icons/TypeScriptSVG.tsx +16 -0
  65. package/src/client/Icons/index.tsx +26 -0
  66. package/src/client/ImageViewer/index.tsx +91 -0
  67. package/src/client/ImageViewer/style.scss +41 -0
  68. package/src/client/PresenceNotifications/PresenceNotifications.test.ts +134 -0
  69. package/src/client/PresenceNotifications/index.tsx +158 -0
  70. package/src/client/PresenceNotifications/style.scss +17 -0
  71. package/src/client/RunCodeWidget/index.tsx +117 -0
  72. package/src/client/RunCodeWidget/style.scss +24 -0
  73. package/src/client/SplitPaneResizeContext.tsx +159 -0
  74. package/src/client/TabList/Tab.tsx +73 -0
  75. package/src/client/TabList/index.tsx +35 -0
  76. package/src/client/TabList/style.scss +51 -0
  77. package/src/client/VZCodeContext/index.tsx +29 -0
  78. package/src/client/VZCodeContext/types.ts +248 -0
  79. package/src/client/VZCodeContext/useVZCodeState.ts +602 -0
  80. package/src/client/VZKeyboardShortcutsDoc.tsx +148 -0
  81. package/src/client/VZLeft.tsx +28 -0
  82. package/src/client/VZMiddle.tsx +172 -0
  83. package/src/client/VZResizer/index.tsx +128 -0
  84. package/src/client/VZResizer/styles.scss +33 -0
  85. package/src/client/VZRight.tsx +113 -0
  86. package/src/client/VZSettings.tsx +275 -0
  87. package/src/client/VZSidebar/AIChat/ChatInput.tsx +243 -0
  88. package/src/client/VZSidebar/AIChat/DiffView.scss +136 -0
  89. package/src/client/VZSidebar/AIChat/DiffView.tsx +176 -0
  90. package/src/client/VZSidebar/AIChat/Message.tsx +79 -0
  91. package/src/client/VZSidebar/AIChat/MessageList.tsx +190 -0
  92. package/src/client/VZSidebar/AIChat/StreamingMessage.tsx +35 -0
  93. package/src/client/VZSidebar/AIChat/ThinkingScratchpad.tsx +156 -0
  94. package/src/client/VZSidebar/AIChat/TypingIndicator.tsx +19 -0
  95. package/src/client/VZSidebar/AIChat/index.tsx +357 -0
  96. package/src/client/VZSidebar/AIChat/styles.scss +646 -0
  97. package/src/client/VZSidebar/AIChat/useSpeechRecognition.ts +117 -0
  98. package/src/client/VZSidebar/CreateDirModal.tsx +111 -0
  99. package/src/client/VZSidebar/CreateFileModal.tsx +111 -0
  100. package/src/client/VZSidebar/DeleteConfirmationModal.tsx +76 -0
  101. package/src/client/VZSidebar/DirectoryListing.tsx +89 -0
  102. package/src/client/VZSidebar/EmptyState.tsx +11 -0
  103. package/src/client/VZSidebar/FileListing.tsx +91 -0
  104. package/src/client/VZSidebar/FileTypeIcon.tsx +49 -0
  105. package/src/client/VZSidebar/Item.tsx +237 -0
  106. package/src/client/VZSidebar/Listing.tsx +47 -0
  107. package/src/client/VZSidebar/Search.tsx +527 -0
  108. package/src/client/VZSidebar/VisualEditor.tsx +282 -0
  109. package/src/client/VZSidebar/VoiceChatModal.tsx +121 -0
  110. package/src/client/VZSidebar/aiCopyPaste.ts +199 -0
  111. package/src/client/VZSidebar/index.tsx +728 -0
  112. package/src/client/VZSidebar/styles.scss +612 -0
  113. package/src/client/VZSidebar/useDragAndDrop.tsx +408 -0
  114. package/src/client/bootstrap.ts +26 -0
  115. package/src/client/buildWorker.ts +3 -0
  116. package/src/client/featureFlags.ts +8 -0
  117. package/src/client/generateRequestId.ts +6 -0
  118. package/src/client/getFileTree.ts +53 -0
  119. package/src/client/index.ts +19 -0
  120. package/src/client/main.jsx +10 -0
  121. package/src/client/presenceColor.ts +8 -0
  122. package/src/client/shouldTriggerRun.ts +31 -0
  123. package/src/client/sortFileTree.ts +21 -0
  124. package/src/client/tabsSearchParameters.test.ts +79 -0
  125. package/src/client/tabsSearchParameters.ts +137 -0
  126. package/src/client/themes/index.ts +7 -0
  127. package/src/client/themes/themes.ts +77 -0
  128. package/src/client/themes/uiwImports.ts +32 -0
  129. package/src/client/themes/useDynamicTheme.ts +29 -0
  130. package/src/client/themes/vizhubTheme/colors.ts +80 -0
  131. package/src/client/themes/vizhubTheme/index.ts +193 -0
  132. package/src/client/useActions.ts +288 -0
  133. package/src/client/useESLint/index.ts +72 -0
  134. package/src/client/useESLint/worker.ts +113 -0
  135. package/src/client/useEditorCache.ts +35 -0
  136. package/src/client/useFileCRUD.ts +228 -0
  137. package/src/client/useKeyboardShortcuts.ts +451 -0
  138. package/src/client/useOpenDirectories.ts +99 -0
  139. package/src/client/usePresenceAutoFollow.ts +85 -0
  140. package/src/client/usePrettier/index.ts +178 -0
  141. package/src/client/usePrettier/worker.ts +109 -0
  142. package/src/client/useRunCode.tsx +15 -0
  143. package/src/client/useRuntimeError.ts +32 -0
  144. package/src/client/useSubmitOperation.ts +15 -0
  145. package/src/client/useURLSync.ts +137 -0
  146. package/src/client/usernameLocalStorage.ts +43 -0
  147. package/src/client/utils/fileExtension.ts +10 -0
  148. package/src/client/utils/isImageFile.ts +12 -0
  149. package/src/client/vzReducer/aiChatReducer.ts +44 -0
  150. package/src/client/vzReducer/closeTabReducer.test.ts +205 -0
  151. package/src/client/vzReducer/closeTabsReducer.ts +96 -0
  152. package/src/client/vzReducer/createInitialState.ts +38 -0
  153. package/src/client/vzReducer/editorNoLongerWantsFocusReducer.ts +9 -0
  154. package/src/client/vzReducer/findPane.ts +19 -0
  155. package/src/client/vzReducer/index.ts +250 -0
  156. package/src/client/vzReducer/openTabReducer.test.ts +127 -0
  157. package/src/client/vzReducer/openTabReducer.ts +74 -0
  158. package/src/client/vzReducer/searchReducer.test.ts +292 -0
  159. package/src/client/vzReducer/searchReducer.ts +181 -0
  160. package/src/client/vzReducer/setActiveFileIdReducer.ts +28 -0
  161. package/src/client/vzReducer/setActiveFileLeftRightReducer.ts +76 -0
  162. package/src/client/vzReducer/setIsDocOpenReducer.ts +9 -0
  163. package/src/client/vzReducer/setIsSettingsOpenReducer.ts +9 -0
  164. package/src/client/vzReducer/setThemeReducer.ts +9 -0
  165. package/src/client/vzReducer/setUsernameReducer.ts +9 -0
  166. package/src/client/vzReducer/splitCurrentPaneReducer.ts +66 -0
  167. package/src/client/vzReducer/toggleAutoFollowReducer.ts +12 -0
  168. package/src/client/vzReducer/updatePane.ts +40 -0
  169. package/src/client/vzReducer/updatePresenceIndicatorReducer.ts +30 -0
  170. package/src/client/vzReducer/visualEditorReducer.ts +9 -0
  171. package/src/ot.ts +24 -0
  172. package/src/randomId.ts +15 -0
  173. package/src/reactRouterExports.ts +15 -0
  174. package/src/runCode.ts +27 -0
  175. package/src/server/aiChatHandler/aiEditing.ts +151 -0
  176. package/src/server/aiChatHandler/chatOperations.ts +476 -0
  177. package/src/server/aiChatHandler/errorHandling.ts +66 -0
  178. package/src/server/aiChatHandler/index.ts +199 -0
  179. package/src/server/aiChatHandler/llmStreaming.ts +419 -0
  180. package/src/server/aiChatHandler/validation.ts +24 -0
  181. package/src/server/computeInitialDocument.ts +220 -0
  182. package/src/server/config.ts +24 -0
  183. package/src/server/featureFlags.ts +8 -0
  184. package/src/server/generateAIResponse.ts +188 -0
  185. package/src/server/handleAIAssist.ts +53 -0
  186. package/src/server/handleAIChatMessage.ts +2 -0
  187. package/src/server/handleAICopilot.ts +97 -0
  188. package/src/server/index.ts +379 -0
  189. package/src/server/isDirectory.ts +1 -0
  190. package/src/server/livekit.ts +24 -0
  191. package/src/server/prettier.ts +115 -0
  192. package/src/server/setupEnv.ts +8 -0
  193. package/src/submitOperation.ts +25 -0
  194. package/src/types.ts +205 -0
  195. package/src/utils/fileDiff.ts +117 -0
  196. package/src/vite-env.d.ts +10 -0
@@ -0,0 +1,174 @@
1
+ import { Extension, RangeSet } from '@codemirror/state';
2
+ import {
3
+ Decoration,
4
+ EditorView,
5
+ ViewPlugin,
6
+ WidgetType,
7
+ } from '@codemirror/view';
8
+ import { colorPickerRegex } from './colorPicker';
9
+
10
+ const colorCircleTheme = EditorView.baseTheme({
11
+ '.color-circle-parent': {
12
+ display: 'inline-block',
13
+ cursor: 'inherit',
14
+ },
15
+ });
16
+
17
+ class ColorWidget extends WidgetType {
18
+ color: string;
19
+ constructor(color: string) {
20
+ super();
21
+ this.color = color;
22
+ }
23
+
24
+ eq(widget: ColorWidget): boolean {
25
+ return widget.color === this.color;
26
+ }
27
+
28
+ toDOM(): HTMLElement {
29
+ const parent = document.createElement('div');
30
+ const size = 20;
31
+ const innerSize = 14;
32
+
33
+ parent.setAttribute(
34
+ 'style',
35
+ `width:${size}px;height:${size}px;position:relative;`,
36
+ // Position relative for the hovering tool tip to work
37
+ );
38
+ parent.className = 'color-circle-parent';
39
+ const svg = document.createElementNS(
40
+ 'http://www.w3.org/2000/svg',
41
+ 'svg',
42
+ );
43
+ const colorCircle = document.createElementNS(
44
+ 'http://www.w3.org/2000/svg',
45
+ 'circle',
46
+ );
47
+ colorCircle.setAttributeNS(
48
+ null,
49
+ 'fill',
50
+ this.color.replace(/["']/g, ''),
51
+ );
52
+ colorCircle.setAttributeNS(null, 'stroke', 'white');
53
+ colorCircle.setAttributeNS(
54
+ null,
55
+ 'stroke-width',
56
+ '0.75',
57
+ );
58
+ colorCircle.setAttributeNS(
59
+ null,
60
+ 'r',
61
+ '' + innerSize / 2,
62
+ );
63
+ colorCircle.setAttributeNS(null, 'cx', '' + size / 2);
64
+ colorCircle.setAttributeNS(
65
+ null,
66
+ 'cy',
67
+ '' + (size / 2 - 1),
68
+ );
69
+
70
+ svg.setAttributeNS(null, 'width', '' + size);
71
+ svg.setAttributeNS(null, 'height', '' + size);
72
+
73
+ svg.appendChild(colorCircle);
74
+
75
+ parent.appendChild(svg);
76
+
77
+ // Hovering tool tip shortcut for when users hover over color circle
78
+ const hoveringTooltip = document.createElement('div');
79
+
80
+ // Set class for tooltip to use CSS styles
81
+ hoveringTooltip.className = 'tooltip_hover_circle';
82
+
83
+ hoveringTooltip.innerHTML = `<strong>Alt + Click on a hex color</strong><br />
84
+ <div>Open a color picker to modify the color</div>`;
85
+
86
+ parent.appendChild(hoveringTooltip);
87
+
88
+ // Timer to prevent constant pop-ups when scrolling through code
89
+ let hoverTimeout: number;
90
+
91
+ // Fade in and fade out effect for when user hovers over the circle
92
+ // Mouse Event to set y and x of the tool tip pop up to prevent overlap onto circle
93
+ parent.addEventListener(
94
+ 'mouseenter',
95
+ (event: MouseEvent) => {
96
+ hoverTimeout = window.setTimeout(() => {
97
+ hoveringTooltip.style.top = `${event.clientY - 60}px`;
98
+ hoveringTooltip.style.left = `${event.clientX}px`;
99
+ hoveringTooltip.style.opacity = '1';
100
+ }, 700);
101
+ },
102
+ );
103
+
104
+ parent.addEventListener('mouseleave', () => {
105
+ clearTimeout(hoverTimeout);
106
+ hoveringTooltip.style.opacity = '0';
107
+ });
108
+
109
+ return parent;
110
+ }
111
+
112
+ ignoreEvent() {
113
+ return false;
114
+ }
115
+ }
116
+
117
+ export const colorsInTextPlugin: Extension = [
118
+ ViewPlugin.fromClass(
119
+ class {
120
+ decorations: any;
121
+ view: EditorView;
122
+ constructor(view: EditorView) {
123
+ this.decorations = RangeSet.of([]);
124
+ this.view = view;
125
+ }
126
+ },
127
+ {
128
+ decorations: (v) => {
129
+ const colorInfos = [];
130
+
131
+ const lines = v.view.state.doc.iter();
132
+ let line = lines.next();
133
+
134
+ // Offset is the number of characters before the hex
135
+ // so the circle can be placed properly.
136
+ let offset = 0;
137
+ while (!line.done) {
138
+ if (line.value === '\n') {
139
+ offset++;
140
+ line = lines.next();
141
+ continue;
142
+ }
143
+ const hexColorOccurences = line.value.matchAll(
144
+ colorPickerRegex,
145
+ );
146
+ let hexOccurance = hexColorOccurences.next();
147
+ while (!hexOccurance.done) {
148
+ const offsetColorInfo = hexOccurance.value;
149
+ offsetColorInfo.index += offset;
150
+ colorInfos.push(offsetColorInfo);
151
+ hexOccurance = hexColorOccurences.next();
152
+ }
153
+ offset += line.value.length;
154
+ line = lines.next();
155
+ }
156
+
157
+ return Decoration.set(
158
+ colorInfos.map((colorInfo) => {
159
+ return {
160
+ // 7 is the length of the hex color string
161
+ from: colorInfo.index + 7,
162
+ to: colorInfo.index + 7,
163
+ value: Decoration.widget({
164
+ side: -1,
165
+ widget: new ColorWidget(colorInfo[0]),
166
+ }),
167
+ };
168
+ }),
169
+ );
170
+ },
171
+ },
172
+ ),
173
+ colorCircleTheme,
174
+ ];
@@ -0,0 +1,79 @@
1
+ // TODO - This file is not being used.
2
+ // It was an attempt to highlight the interactive widgets in the editor.
3
+ // It is a good idea to keep this file for future reference, as it's
4
+ // pretty close to what we need to implement. It suffers from the following
5
+ // issues:
6
+ // - Hardcoded regexes for interactive widgets - we can pull from the various modules
7
+ // - The highlight is not working properly - it's not un-highlighting
8
+ // when the Alt key is released. This is likely due to the fact that the
9
+ // plugin is not being updated when the key is released.
10
+
11
+ // export const highlightWidgets = ViewPlugin.fromClass(
12
+ // class {
13
+ // showHighlight: boolean;
14
+ // view: EditorView;
15
+ // constructor(view: EditorView) {
16
+ // this.showHighlight = false;
17
+ // this.view = view;
18
+ // }
19
+ // },
20
+ // {
21
+ // decorations: (v) => {
22
+ // if (!v.showHighlight) {
23
+ // return Decoration.none;
24
+ // }
25
+
26
+ // const interactOpportunities = [];
27
+ // const lines = v.view.state.doc.iter();
28
+ // let line = lines.next();
29
+
30
+ // //Offset is the number of characters before the regex so the highlighting can be placed properly.
31
+ // let offset = 0;
32
+ // while (!line.done) {
33
+ // if (line.value === '\n') {
34
+ // offset++;
35
+ // line = lines.next();
36
+ // continue;
37
+ // }
38
+ // const interactiveOccurances = line.value.matchAll(
39
+ // //The below line contains all of the Regexes for all of the interactive widgets.
40
+ // /\"\#([0-9]|[A-F]|[a-f]){6}\"|rotate\(-?\d*\.?\d*\)|https?:\/\/[^ "]+|vec2\(-?\b\d+\.?\d*\b\s*(,\s*-?\b\d+\.?\d*\b)?\)|true|false|(?<!\#)-?\b\d+\.?\d*\b|rgb\(.*\)/dg,
41
+ // );
42
+ // let interactOccurance =
43
+ // interactiveOccurances.next();
44
+ // while (!interactOccurance.done) {
45
+ // const offsetInteract = interactOccurance.value;
46
+ // offsetInteract.indices[0][0] += offset;
47
+ // offsetInteract.indices[0][1] += offset;
48
+ // interactOpportunities.push(offsetInteract);
49
+ // interactOccurance = interactiveOccurances.next();
50
+ // }
51
+ // offset += line.value.length;
52
+ // line = lines.next();
53
+ // }
54
+ // return Decoration.set(
55
+ // interactOpportunities.map((opportunity) => {
56
+ // return {
57
+ // from: opportunity.indices[0][0],
58
+ // to: opportunity.indices[0][1],
59
+ // value: Decoration.mark({
60
+ // class: 'cm-interact ',
61
+ // }),
62
+ // };
63
+ // }),
64
+ // );
65
+ // },
66
+ // eventHandlers: {
67
+ // keydown(event, view) {
68
+ // if (event.key == 'Alt') {
69
+ // this.showHighlight = true;
70
+ // }
71
+ // },
72
+ // keyup(event, view) {
73
+ // if (event.key == 'Alt') {
74
+ // this.showHighlight = false;
75
+ // }
76
+ // },
77
+ // },
78
+ // },
79
+ // );
@@ -0,0 +1,44 @@
1
+ import interact, {
2
+ InteractRule,
3
+ } from '@replit/codemirror-interact';
4
+ import { urlClicker } from './urlClicker';
5
+ import { colorPicker } from './colorPicker';
6
+ import { numberDragger } from './numberDragger';
7
+ import { boolToggler } from './boolToggler';
8
+ import { vec2Slider } from './vec2Slider';
9
+ import { rotateWidget } from './rotateWidget';
10
+ import { markdownCheckboxToggler } from './markdownCheckboxToggler';
11
+
12
+ // Additional supporting CodeMirror plugins.
13
+ export { colorsInTextPlugin } from './colorsInTextPlugin';
14
+ export { rotationIndicator } from './rotateWidget';
15
+
16
+ // Interactive code widgets.
17
+ // * Number dragger
18
+ // * Boolean toggler
19
+ // * URL clicker
20
+ // * color picker
21
+ // Inspired by:
22
+ // https://github.com/replit/codemirror-interact/blob/master/dev/index.ts
23
+ // `onInteract` is called when the user interacts with a widget.
24
+ export const widgets = ({
25
+ onInteract,
26
+ customInteractRules,
27
+ }: {
28
+ onInteract?: () => void;
29
+ customInteractRules?: Array<InteractRule>;
30
+ }) => {
31
+ const rules: Array<InteractRule> = [
32
+ colorPicker(onInteract),
33
+ numberDragger(onInteract),
34
+ markdownCheckboxToggler(onInteract),
35
+ boolToggler(onInteract),
36
+ vec2Slider(onInteract),
37
+ rotateWidget(onInteract),
38
+ urlClicker,
39
+ ];
40
+ if (customInteractRules) {
41
+ rules.push(...customInteractRules);
42
+ }
43
+ return interact({ rules });
44
+ };
@@ -0,0 +1,12 @@
1
+ import { InteractRule } from '@replit/codemirror-interact';
2
+
3
+ export const markdownCheckboxToggler = (
4
+ onInteract?: () => void,
5
+ ): InteractRule => ({
6
+ regexp: /\[[ x]\]/g, // Matches [ ], [x], [X]
7
+ cursor: 'pointer',
8
+ onClick: (text, setText) => {
9
+ setText(text === '[ ]' ? '[x]' : '[ ]');
10
+ if (onInteract) onInteract();
11
+ },
12
+ });
@@ -0,0 +1,20 @@
1
+ import { InteractRule } from '@replit/codemirror-interact';
2
+
3
+ const numberDraggerRegex = /(?<!\#)-?\b\d+\.?\d*\b/g;
4
+
5
+ // a rule for a number dragger
6
+ export const numberDragger = (
7
+ onInteract?: () => void,
8
+ ): InteractRule => ({
9
+ // the regexp matching the value
10
+ regexp: numberDraggerRegex,
11
+ // set cursor to "ew-resize" on hover
12
+ cursor: 'ew-resize',
13
+ // change number value based on mouse X movement on drag
14
+ onDrag: (text, setText, e) => {
15
+ const newVal = Number(text) + e.movementX;
16
+ if (isNaN(newVal)) return;
17
+ setText(newVal.toString());
18
+ if (onInteract) onInteract();
19
+ },
20
+ });
@@ -0,0 +1,143 @@
1
+ import {
2
+ Decoration,
3
+ EditorView,
4
+ ViewPlugin,
5
+ WidgetType,
6
+ } from '@codemirror/view';
7
+ import { InteractRule } from '@replit/codemirror-interact';
8
+
9
+ let rotationOrigin: { x: number; y: number } = null;
10
+ // Set rotation to the angle between the x-axis and a line
11
+ // from the word "rotate" to the mouse pointer (while dragging).
12
+ // The rotation is in range (-180,180]
13
+ export const rotateWidget = (
14
+ onInteract?: () => void,
15
+ ): InteractRule => ({
16
+ regexp: /rotate\(-?\d*\.?\d*\)/g,
17
+ cursor: 'move',
18
+ onDragStart(text, setText, e) {
19
+ rotationOrigin = { x: e.clientX, y: e.clientY };
20
+ },
21
+
22
+ onDrag(text, setText, e) {
23
+ if (rotationOrigin == null) return;
24
+ const rotationDegree = Math.round(
25
+ (Math.atan2(
26
+ rotationOrigin.y - e.clientY,
27
+ e.clientX - rotationOrigin.x,
28
+ ) *
29
+ 180) /
30
+ Math.PI,
31
+ );
32
+ //Calculate the angle between the x axis and a line from where the user first clicks to the current location of the mouse.
33
+ setText(`rotate(${rotationDegree})`);
34
+ const updateDragEvent = new CustomEvent(
35
+ 'updateRotateDrag',
36
+ { detail: rotationDegree },
37
+ );
38
+
39
+ document.dispatchEvent(updateDragEvent);
40
+
41
+ if (onInteract) onInteract();
42
+ },
43
+ onDragEnd() {
44
+ rotationOrigin = null;
45
+ },
46
+ });
47
+
48
+ export const rotationIndicator = ViewPlugin.fromClass(
49
+ class {
50
+ view: EditorView;
51
+ textPosition?: number;
52
+ rotation: number;
53
+ constructor(view) {
54
+ this.view = view;
55
+ this.textPosition = null;
56
+ this.rotation = 0;
57
+
58
+ document.addEventListener(
59
+ 'updateRotateDrag',
60
+ (e: CustomEvent) => {
61
+ this.textPosition = this.view.posAtCoords(
62
+ rotationOrigin,
63
+ false,
64
+ );
65
+
66
+ this.rotation = e.detail;
67
+ },
68
+ );
69
+ }
70
+ },
71
+ {
72
+ decorations: (v) => {
73
+ if (rotationOrigin === null) {
74
+ return Decoration.none;
75
+ }
76
+ return Decoration.set([
77
+ {
78
+ from: v.textPosition,
79
+ to: v.textPosition,
80
+ value: Decoration.widget({
81
+ side: -1,
82
+ widget: new RotationCircle(v.rotation),
83
+ }),
84
+ },
85
+ ]);
86
+ },
87
+ },
88
+ );
89
+
90
+ class RotationCircle extends WidgetType {
91
+ angle: number;
92
+ constructor(angle: number) {
93
+ super();
94
+ this.angle = angle;
95
+ }
96
+
97
+ toDOM(view: EditorView): HTMLElement {
98
+ const parent = document.createElement('div');
99
+
100
+ parent.setAttribute('style', 'width:20px;height:20px');
101
+ parent.className = 'color-circle-parent';
102
+ const svg = document.createElementNS(
103
+ 'http://www.w3.org/2000/svg',
104
+ 'svg',
105
+ );
106
+ const colorCircle = document.createElementNS(
107
+ 'http://www.w3.org/2000/svg',
108
+ 'circle',
109
+ );
110
+ colorCircle.setAttributeNS(null, 'fill', '#808080');
111
+ colorCircle.setAttributeNS(null, 'r', '5');
112
+ colorCircle.setAttributeNS(null, 'cx', '5');
113
+ colorCircle.setAttributeNS(null, 'cy', '5');
114
+
115
+ const indicatorLine = document.createElementNS(
116
+ 'http://www.w3.org/2000/svg',
117
+ 'line',
118
+ );
119
+ indicatorLine.setAttributeNS(null, 'x1', '10');
120
+ indicatorLine.setAttributeNS(null, 'x2', '5');
121
+ indicatorLine.setAttributeNS(null, 'y1', '5');
122
+ indicatorLine.setAttributeNS(null, 'y2', '5');
123
+ indicatorLine.setAttributeNS(null, 'stroke', 'black');
124
+ indicatorLine.setAttributeNS(
125
+ null,
126
+ 'transform',
127
+ `rotate(${360 - this.angle},5,5)`,
128
+ );
129
+
130
+ svg.setAttributeNS(null, 'viewBox', '0 0 10 10');
131
+ svg.setAttributeNS(null, 'width', '20');
132
+ svg.setAttributeNS(null, 'height', '20');
133
+
134
+ svg.appendChild(colorCircle);
135
+ svg.appendChild(indicatorLine);
136
+
137
+ parent.appendChild(svg);
138
+ return parent;
139
+ }
140
+ ignoreEvent() {
141
+ return false;
142
+ }
143
+ }
@@ -0,0 +1,48 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { urlClickerRegex } from './urlClicker';
3
+
4
+ describe('URL regex', () => {
5
+ test.each([
6
+ {
7
+ name: 'standard URL',
8
+ input:
9
+ 'Visit https://example.com for more information.',
10
+ expected: ['https://example.com'],
11
+ },
12
+ {
13
+ name: 'URL with single quote at the end',
14
+ input: "Click here: https://example.com';",
15
+ expected: ['https://example.com'],
16
+ },
17
+ {
18
+ name: 'multiple URLs',
19
+ input:
20
+ 'Check out https://site1.com and https://site2.com.',
21
+ expected: ['https://site1.com', 'https://site2.com'],
22
+ },
23
+ {
24
+ name: 'URL with query parameters',
25
+ input:
26
+ 'Visit https://example.com?param1=value1&param2=value2.',
27
+ expected: [
28
+ 'https://example.com?param1=value1&param2=value2',
29
+ ],
30
+ },
31
+ {
32
+ name: 'URL with parentheses',
33
+ input:
34
+ 'See the map at https://example.com/maps?location=(123,456).',
35
+ expected: [
36
+ 'https://example.com/maps?location=(123,456)',
37
+ ],
38
+ },
39
+ {
40
+ name: 'no URL present',
41
+ input: 'This text does not contain a URL.',
42
+ expected: [],
43
+ },
44
+ ])('$name', ({ input, expected }) => {
45
+ const matches = input.match(urlClickerRegex) || [];
46
+ expect(matches).toEqual(expected);
47
+ });
48
+ });
@@ -0,0 +1,12 @@
1
+ import { InteractRule } from '@replit/codemirror-interact';
2
+
3
+ export const urlClickerRegex =
4
+ /https?:\/\/[^\s'")]*([^\s'",;.]|\([^\s'")]*\)|\b)/g;
5
+
6
+ export const urlClicker: InteractRule = {
7
+ regexp: urlClickerRegex,
8
+ cursor: 'pointer',
9
+ onClick: (text: string) => {
10
+ window.open(text);
11
+ },
12
+ };
@@ -0,0 +1,25 @@
1
+ // vec2 slider
2
+ // Inspired by: https://github.com/replit/codemirror-interact/blob/master/dev/index.ts#L61
3
+
4
+ import { InteractRule } from '@replit/codemirror-interact';
5
+
6
+ export const vec2Slider = (
7
+ onInteract?: () => void,
8
+ ): InteractRule => ({
9
+ regexp:
10
+ /vec2\(-?\b\d+\.?\d*\b\s*(,\s*-?\b\d+\.?\d*\b)?\)/g,
11
+ cursor: 'move',
12
+
13
+ onDrag: (text, setText, e) => {
14
+ const res =
15
+ /vec2\((?<x>-?\b\d+\.?\d*\b)\s*(,\s*(?<y>-?\b\d+\.?\d*\b))?\)/.exec(
16
+ text,
17
+ );
18
+ const x = Number(res?.groups?.x);
19
+ let y = Number(res?.groups?.y);
20
+ if (isNaN(x)) return;
21
+ if (isNaN(y)) y = x;
22
+ setText(`vec2(${x + e.movementX}, ${y - e.movementY})`);
23
+ if (onInteract) onInteract();
24
+ },
25
+ });
@@ -0,0 +1,73 @@
1
+ // Customized "basic setup" for CodeMirror.
2
+ // Mainly customized to remove keymaps, for compatibility with
3
+ // codemirror-vscode-keymap.
4
+ //
5
+ // Inspired by
6
+ // https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts
7
+ // https://github.com/replit/codemirror-vscode-keymap/blob/master/dev/basicSetupNoKeymap.ts
8
+ import {
9
+ highlightSpecialChars,
10
+ drawSelection,
11
+ highlightActiveLine,
12
+ dropCursor,
13
+ rectangularSelection,
14
+ crosshairCursor,
15
+ lineNumbers,
16
+ highlightActiveLineGutter,
17
+ } from '@codemirror/view';
18
+ import { Extension, EditorState } from '@codemirror/state';
19
+ import {
20
+ defaultHighlightStyle,
21
+ syntaxHighlighting,
22
+ indentOnInput,
23
+ bracketMatching,
24
+ } from '@codemirror/language';
25
+ import { history } from '@codemirror/commands';
26
+ import { highlightSelectionMatches } from '@codemirror/search';
27
+ import {
28
+ autocompletion,
29
+ closeBrackets,
30
+ } from '@codemirror/autocomplete';
31
+
32
+ export const basicSetup: Extension = (() => [
33
+ lineNumbers(),
34
+ highlightActiveLineGutter(),
35
+ highlightSpecialChars(),
36
+ history(),
37
+
38
+ // Not really a fan of foldGutter.
39
+ // Unclear if it's really useful.
40
+ // If we ever want to use it, we should
41
+ // make sure the styling is decent.
42
+ // foldGutter(),
43
+ drawSelection(),
44
+ dropCursor(),
45
+ EditorState.allowMultipleSelections.of(true),
46
+ indentOnInput(),
47
+ syntaxHighlighting(defaultHighlightStyle, {
48
+ fallback: true,
49
+ }),
50
+ bracketMatching(),
51
+ closeBrackets(),
52
+ // We set this to `false` because that is prescribed by
53
+ // https://github.com/replit/codemirror-vscode-keymap
54
+ autocompletion({ defaultKeymap: false }),
55
+
56
+ rectangularSelection(),
57
+ crosshairCursor(),
58
+ highlightActiveLine(),
59
+ highlightSelectionMatches(),
60
+
61
+ // All of these keymaps are disabled because we use
62
+ // codemirror-vscode-keymap, which provides its own
63
+ // keymaps for all of these things.
64
+ // keymap.of([
65
+ // ...closeBracketsKeymap,
66
+ // ...defaultKeymap,
67
+ // ...searchKeymap,
68
+ // ...historyKeymap,
69
+ // // ...foldKeymap,
70
+ // ...completionKeymap,
71
+ // ...lintKeymap,
72
+ // ]),
73
+ ])();