|
| 1 | +import { computed, onUnmounted, ref, watch } from 'vue' |
| 2 | +import { PORT } from '../config' |
| 3 | +import { useYjs } from './useYjs' |
| 4 | +import { MonacoBinding } from 'y-monaco' |
| 5 | +import type { UserAwareness } from '../type' |
| 6 | + |
| 7 | +interface UseCollabMonacoOptions { |
| 8 | + currentUser: UserAwareness |
| 9 | + editorRef: any |
| 10 | + roomId: string |
| 11 | + fieldName: string |
| 12 | +} |
| 13 | + |
| 14 | +function makeTransparent(hex: string, alpha: number): string { |
| 15 | + // hex: "#RRGGBB" |
| 16 | + if (!hex.startsWith('#') || (hex.length !== 7 && hex.length !== 4)) return hex |
| 17 | + let r: number, g: number, b: number |
| 18 | + |
| 19 | + if (hex.length === 7) { |
| 20 | + r = parseInt(hex.slice(1, 3), 16) |
| 21 | + g = parseInt(hex.slice(3, 5), 16) |
| 22 | + b = parseInt(hex.slice(5, 7), 16) |
| 23 | + } else { |
| 24 | + // "#RGB" 短写 |
| 25 | + r = parseInt(hex[1] + hex[1], 16) |
| 26 | + g = parseInt(hex[2] + hex[2], 16) |
| 27 | + b = parseInt(hex[3] + hex[3], 16) |
| 28 | + } |
| 29 | + |
| 30 | + return `rgba(${r},${g},${b},${alpha})` |
| 31 | +} |
| 32 | + |
| 33 | +export function useCollabMonaco(options: UseCollabMonacoOptions) { |
| 34 | + const { currentUser, editorRef, roomId, fieldName } = options |
| 35 | + const { ydoc, awareness } = useYjs(roomId, { websocketUrl: `ws://localhost:${PORT}` }) |
| 36 | + |
| 37 | + const monacoBinding = ref<MonacoBinding | null>(null) |
| 38 | + |
| 39 | + watch( |
| 40 | + () => [awareness.value, editorRef.value], |
| 41 | + ([yjsAwareness, monacoComponent]) => { |
| 42 | + if (yjsAwareness && monacoComponent && !monacoBinding.value) { |
| 43 | + const editor = monacoComponent.getEditor() |
| 44 | + if (!editor) { |
| 45 | + // eslint-disable-next-line no-console |
| 46 | + console.error('[useMonacoCollab] 无法从 monacoComponent 获取 editor 实例。') |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + const model = editor.getModel() |
| 51 | + if (!model) return |
| 52 | + |
| 53 | + yjsAwareness.setLocalStateField('user', { |
| 54 | + name: currentUser.name, |
| 55 | + id: currentUser.id, |
| 56 | + color: currentUser.color, |
| 57 | + colorLight: makeTransparent(currentUser.color, 0.2) |
| 58 | + }) |
| 59 | + |
| 60 | + monacoBinding.value = new MonacoBinding(ydoc.getText(fieldName), model, new Set([editor]), yjsAwareness) |
| 61 | + |
| 62 | + // eslint-disable-next-line no-console |
| 63 | + console.log('[useMonacoCollab] Yjs 绑定成功!光标同步已自动激活。') |
| 64 | + |
| 65 | + // 监听 awareness,添加用户名标签 |
| 66 | + // const handleAwarenessUpdate = () => { |
| 67 | + // const states = (yjsAwareness as Awareness).getStates() |
| 68 | + // const decorations: monaco.editor.IModelDeltaDecoration[] = [] |
| 69 | + |
| 70 | + // states.forEach((state, clientId) => { |
| 71 | + // // 跳过自己和没有用户和光标信息的 |
| 72 | + // if (clientId === yjsAwareness.clientId || !state.selection || !state.user) { |
| 73 | + // return |
| 74 | + // } |
| 75 | + |
| 76 | + // // y-monaco 已经创建了光标 找到其位置即可 |
| 77 | + // const headPos = model.getPositionAt(state.selection.head) |
| 78 | + |
| 79 | + // // 添加一个 "用户标签" 的 decoration |
| 80 | + // decorations.push({ |
| 81 | + // range: new monaco.Range(headPos.lineNumber, headPos.column, headPos.lineNumber, headPos.column), |
| 82 | + // options: { |
| 83 | + // after: { |
| 84 | + // content: state.user.name, |
| 85 | + // // 添加一个类名,用于设置样式 |
| 86 | + // inlineClassName: 'y-remote-cursor-username' |
| 87 | + // }, |
| 88 | + // // 设置一个唯一的 CSS 类名,用于动态应用颜色 |
| 89 | + // inlineClassName: `y-remote-user-color-${clientId}` |
| 90 | + // } |
| 91 | + // }) |
| 92 | + // }) |
| 93 | + |
| 94 | + // editor.deltaDecorations([], decorations) |
| 95 | + // } |
| 96 | + |
| 97 | + // yjsAwareness.on('update', handleAwarenessUpdate) |
| 98 | + // handleAwarenessUpdate() // 初始渲染 |
| 99 | + } |
| 100 | + }, |
| 101 | + { immediate: true } |
| 102 | + ) |
| 103 | + |
| 104 | + onUnmounted(() => { |
| 105 | + monacoBinding.value?.destroy() |
| 106 | + if (awareness.value) { |
| 107 | + awareness.value.setLocalStateField('user', null) |
| 108 | + } |
| 109 | + }) |
| 110 | + |
| 111 | + return { |
| 112 | + isBindingReady: computed(() => monacoBinding.value !== null) |
| 113 | + } |
| 114 | +} |
0 commit comments