This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 376
feat: Add FieldToolbar to expression fields #6573
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ab527f0
feat: Add field toolbar to expressions
tdurnford 8fed34f
feat: Add FieldToolbar to expression fields
tdurnford 2bb68be
Merge branch 'main' into durnford/feat/field-toolbar
tdurnford 4632e1b
changes
sorgh 9895c14
update callout
tdurnford 85c9cd6
fixes
sorgh 5ce202b
re-factor and more
sorgh c664fb3
Merge branch 'main' into durnford/feat/field-toolbar
hatpick 87ecf5f
deps
sorgh c28e9c9
format
sorgh 2a8f307
fix test
tdurnford b16b80c
Merge branch 'durnford/feat/field-toolbar' of github.com:microsoft/Bo…
tdurnford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 83 additions & 49 deletions
132
Composer/packages/adaptive-form/src/components/expressions/ExpressionsListMenu.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,62 +1,96 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { ContextualMenu, DirectionalHint } from 'office-ui-fabric-react/lib/ContextualMenu'; | ||
| import React, { useCallback, useMemo } from 'react'; | ||
| import { builtInFunctionsGrouping, getBuiltInFunctionInsertText } from '@bfc/built-in-functions'; | ||
|
|
||
| import { expressionGroupingsToMenuItems } from './utils/expressionsListMenuUtils'; | ||
|
|
||
| const componentMaxHeight = 400; | ||
| import { DirectionalHint } from 'office-ui-fabric-react/lib/ContextualMenu'; | ||
| import React from 'react'; | ||
| import { Callout } from 'office-ui-fabric-react/lib/Callout'; | ||
| import { FieldToolbar } from '@bfc/code-editor'; | ||
| import { useShellApi } from '@bfc/extension-client'; | ||
|
|
||
| type ExpressionsListMenuProps = { | ||
| onExpressionSelected: (expression: string) => void; | ||
| onMenuMount: (menuContainerElms: HTMLDivElement[]) => void; | ||
| container: HTMLDivElement | null; | ||
| target: HTMLInputElement | HTMLTextAreaElement | null; | ||
|
tdurnford marked this conversation as resolved.
|
||
| clearTarget: () => void; | ||
|
hatpick marked this conversation as resolved.
Outdated
|
||
| value?: string; | ||
| onChange: (expression: string) => void; | ||
| }; | ||
|
|
||
| export const ExpressionsListMenu = (props: ExpressionsListMenuProps) => { | ||
| const { onExpressionSelected, onMenuMount } = props; | ||
| const { clearTarget, container, target, value = '', onChange } = props; | ||
| const { projectId, shellApi } = useShellApi(); | ||
|
|
||
| const containerRef = React.createRef<HTMLDivElement>(); | ||
| const [memoryVariables, setMemoryVariables] = React.useState<string[] | undefined>(); | ||
|
|
||
| const onExpressionKeySelected = useCallback( | ||
| (key) => { | ||
| const insertText = getBuiltInFunctionInsertText(key); | ||
| onExpressionSelected('= ' + insertText); | ||
| }, | ||
| [onExpressionSelected] | ||
| ); | ||
| React.useEffect(() => { | ||
| const abortController = new AbortController(); | ||
| (async () => { | ||
| try { | ||
| const variables = await shellApi.getMemoryVariables(projectId, { signal: abortController.signal }); | ||
| setMemoryVariables(variables); | ||
| } catch (e) { | ||
| // error can be due to abort | ||
| } | ||
| })(); | ||
| }, [projectId]); | ||
|
|
||
| const onLayerMounted = useCallback(() => { | ||
| const elms = document.querySelectorAll<HTMLDivElement>('.ms-ContextualMenu-Callout'); | ||
| onMenuMount(Array.prototype.slice.call(elms)); | ||
| }, [onMenuMount]); | ||
|
|
||
| const menuItems = useMemo( | ||
| () => | ||
| expressionGroupingsToMenuItems( | ||
| builtInFunctionsGrouping, | ||
| onExpressionKeySelected, | ||
| onLayerMounted, | ||
| componentMaxHeight | ||
| ), | ||
| [onExpressionKeySelected, onLayerMounted] | ||
| ); | ||
| React.useEffect(() => { | ||
| const keyDownHandler = (e: KeyboardEvent) => { | ||
| if (e.key === 'Escape') { | ||
|
hatpick marked this conversation as resolved.
Outdated
|
||
| clearTarget(); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div ref={containerRef}> | ||
| <ContextualMenu | ||
| calloutProps={{ | ||
| onLayerMounted: onLayerMounted, | ||
| }} | ||
| directionalHint={DirectionalHint.bottomLeftEdge} | ||
| hidden={false} | ||
| items={menuItems} | ||
| shouldFocusOnMount={false} | ||
| styles={{ | ||
| container: { maxHeight: componentMaxHeight }, | ||
| }} | ||
| target={containerRef} | ||
| /> | ||
| </div> | ||
| const focusHandler = (e: FocusEvent) => { | ||
| if (container?.contains(e.target as Node)) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| !e | ||
| .composedPath() | ||
| .filter((n) => n instanceof Element) | ||
| .map((n) => (n as Element).className) | ||
| .some((c) => c.indexOf('js-lg-toolbar-menu') !== -1) | ||
|
hatpick marked this conversation as resolved.
Outdated
|
||
| ) { | ||
| clearTarget(); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('focusin', focusHandler); | ||
| document.addEventListener('keydown', keyDownHandler); | ||
|
|
||
| return () => { | ||
| document.removeEventListener('focusin', focusHandler); | ||
| document.removeEventListener('keydown', keyDownHandler); | ||
| }; | ||
| }, [clearTarget, container]); | ||
|
|
||
| const onSelectToolbarMenuItem = React.useCallback( | ||
| (text: string) => { | ||
| if (typeof target?.selectionStart === 'number') { | ||
| const start = target.selectionStart; | ||
| const end = typeof target?.selectionEnd === 'number' ? target.selectionEnd : target.selectionStart; | ||
|
|
||
| const updatedItem = [value.slice(0, start), text, value.slice(end)].join(''); | ||
| onChange(updatedItem); | ||
|
|
||
| setTimeout(() => { | ||
| target.setSelectionRange(updatedItem.length, updatedItem.length); | ||
| }, 0); | ||
| } | ||
|
|
||
| target?.focus(); | ||
| }, | ||
| [target, value, onChange] | ||
| ); | ||
| return target ? ( | ||
| <Callout directionalHint={DirectionalHint.topLeftEdge} gapSpace={2} isBeakVisible={false} target={target}> | ||
| <FieldToolbar | ||
| key="lg-toolbar" | ||
|
tdurnford marked this conversation as resolved.
Outdated
|
||
| excludedToolbarItems={['template']} | ||
| properties={memoryVariables} | ||
| onSelectToolbarMenuItem={onSelectToolbarMenuItem} | ||
| /> | ||
| </Callout> | ||
| ) : null; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.