-
-
Notifications
You must be signed in to change notification settings - Fork 213
fix: add type definitions, extract hook, and implement security harde… #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
336c2da
b2da04a
418bf33
d5fac52
5608361
dae227d
8611740
368875b
b025235
4b4cd1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { useState } from 'react'; | ||
| import * as monaco from 'monaco-editor'; | ||
| import useAppStore from '../store/store'; | ||
| import CodeSelectionMenu from '../components/CodeSelectionMenu'; | ||
|
|
||
| export const useCodeSelection = (editorType: 'markdown' | 'concerto' | 'json') => { | ||
| const [selectedText, setSelectedText] = useState(''); | ||
| const [menuPosition, setMenuPosition] = useState<{ x: number; y: number } | null>(null); | ||
| const [showMenu, setShowMenu] = useState(false); | ||
| const enableCodeSelectionMenu = useAppStore((state) => state.aiConfig?.enableCodeSelectionMenu ?? true); | ||
|
|
||
|
|
||
| const handleSelection = (editor: monaco.editor.IStandaloneCodeEditor) => { | ||
| const selection = editor.getSelection(); | ||
| if (selection && !selection.isEmpty()) { | ||
| const selectedText = editor.getModel()?.getValueInRange(selection).trim(); | ||
| if (selectedText && selectedText.length > 0) { | ||
| const position = editor.getScrolledVisiblePosition(selection.getStartPosition()); | ||
| const editorContainer = editor.getDomNode()?.closest('.editorwrapper'); | ||
| const editorRect = editorContainer?.getBoundingClientRect(); | ||
|
|
||
| let x: number, y: number; | ||
|
|
||
| if (editorRect && position) { | ||
| x = Math.max(editorRect.left + 20, Math.min(position.left, editorRect.right - 150)); | ||
| y = Math.max(editorRect.top + 20, Math.min(position.top, editorRect.bottom - 50)); | ||
|
|
||
| x = Math.max(10, Math.min(x, window.innerWidth - 150)); | ||
| y = Math.max(10, Math.min(y, window.innerHeight - 50)); | ||
| } else if (position) { | ||
| x = Math.max(10, Math.min(position.left, window.innerWidth - 150)); | ||
| y = Math.max(10, Math.min(position.top, window.innerHeight - 50)); | ||
| } else { | ||
| return; | ||
| } | ||
|
|
||
| setSelectedText(selectedText); | ||
| setMenuPosition({ | ||
| x: x, | ||
| y: y | ||
| }); | ||
| setShowMenu(true); | ||
| } | ||
| } else { | ||
| setShowMenu(false); | ||
| } | ||
| } | ||
|
|
||
| const closeMenu = () => { | ||
| setShowMenu(false); | ||
| setSelectedText(''); | ||
| setMenuPosition(null); | ||
| }; | ||
|
|
||
| const MenuComponent = (enableCodeSelectionMenu && showMenu && selectedText && menuPosition) ? ( | ||
| <CodeSelectionMenu | ||
| selectedText={selectedText} | ||
| position={menuPosition} | ||
| onClose={closeMenu} | ||
| editorType={editorType} | ||
| /> | ||
| ) : null; | ||
|
||
|
|
||
| return { | ||
| handleSelection, | ||
| closeMenu, | ||
| MenuComponent | ||
| }; | ||
| }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should move typescript typedef upstream to the @accordproject/markdown-template package. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| declare module '@accordproject/markdown-template' { | ||
| import { ModelManager } from '@accordproject/concerto-core'; | ||
|
|
||
| export class TemplateMarkTransformer { | ||
| fromMarkdownTemplate(options: { | ||
| fileName: string; | ||
| content: string; | ||
| modelManager: ModelManager; | ||
| templateKind?: string; | ||
| }): { getModelManager: () => ModelManager }; | ||
|
|
||
| toMarkdownTemplate(parserManager: { getModelManager: () => ModelManager }): string; | ||
|
|
||
| generate(data: Record<string, unknown>, parserManager: { getModelManager: () => ModelManager }): string; | ||
|
|
||
| getVariables(parserManager: { getModelManager: () => ModelManager }): Record<string, unknown>; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.