-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-components): poi architecture changes (#4859)
* chore: implement POI ADS provider * feat(react-components): PoI-related architecture changes * chore: add missing export * chore: lint fix * chore: revert PoI-changes (not relevant in this PR) * chore: typing fix * chore: modify BaseInputCommand to be a base class * chore: fix according to feedback * chore: lint fix, fix compiler errors * chore: add missing export * chore: accept undefined subject in update hooks
- Loading branch information
1 parent
f5ac8c8
commit fcd8766
Showing
21 changed files
with
459 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
46 changes: 46 additions & 0 deletions
46
react-components/src/architecture/base/commands/BaseInputCommand.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type TranslateDelegate, type TranslateKey } from '../utilities/TranslateKey'; | ||
import { RenderTargetCommand } from './RenderTargetCommand'; | ||
|
||
export abstract class BaseInputCommand extends RenderTargetCommand { | ||
protected _placeholder?: TranslateKey; | ||
protected _content?: string; | ||
protected _okButtonLabel?: TranslateKey; | ||
|
||
protected _onFinish?: () => void; | ||
protected _onCancel?: () => void; | ||
|
||
public getCancelButtonLabel(_t: TranslateDelegate): string | undefined { | ||
return undefined; | ||
} | ||
|
||
public abstract getPostButtonLabel(t: TranslateDelegate): string | undefined; | ||
|
||
public abstract getPlaceholder(t: TranslateDelegate): string | undefined; | ||
|
||
public get onFinish(): (() => void) | undefined { | ||
return this._onFinish; | ||
} | ||
|
||
public set onFinish(onFinish: () => void) { | ||
this._onFinish = onFinish; | ||
} | ||
|
||
public set onCancel(onCancel: () => void) { | ||
this._onCancel = onCancel; | ||
} | ||
|
||
public get onCancel(): (() => void) | undefined { | ||
return this._onCancel; | ||
} | ||
|
||
invokeWithContent(content: string): boolean { | ||
this._content = content; | ||
|
||
const invokeResult = this.invoke(); | ||
this._onFinish?.(); | ||
return invokeResult; | ||
} | ||
} |
This file contains 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 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 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
34 changes: 34 additions & 0 deletions
34
react-components/src/architecture/base/reactUpdaters/AnchoredDialogUpdater.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
|
||
export type SetCounterDelegate = (counter: number) => void; | ||
|
||
export class AnchoredDialogUpdater { | ||
// ================================================== | ||
// STATIC FIELDS | ||
// ================================================== | ||
|
||
private static _setCounter: SetCounterDelegate | undefined = undefined; | ||
private static _counter = 0; | ||
|
||
// ================================================== | ||
// STATIC METHODS | ||
// ================================================== | ||
|
||
public static setCounterDelegate(value: SetCounterDelegate | undefined): void { | ||
this._setCounter = value; | ||
} | ||
|
||
public static update(): void { | ||
// Increment the counter, so the state change in React and force a redraw each time the active tool changes | ||
// The reason for solution it that I only want to store the active tool at one single location, since this gives a more | ||
// stable code, and never goes out of sync. | ||
// React get the active tool by: renderTarget.commandsController.activeTool; | ||
if (this._setCounter === undefined) { | ||
return; | ||
} | ||
this._counter++; | ||
this._setCounter(this._counter); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
react-components/src/architecture/base/reactUpdaters/ContextMenuUpdater.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
|
||
export type SetCounterDelegate = (counter: number) => void; | ||
|
||
export class ContextMenuUpdater { | ||
// ================================================== | ||
// STATIC FIELDS | ||
// ================================================== | ||
|
||
private static _setCounter: SetCounterDelegate | undefined = undefined; | ||
private static _counter = 0; | ||
|
||
// ================================================== | ||
// STATIC METHODS | ||
// ================================================== | ||
|
||
public static setCounterDelegate(value: SetCounterDelegate | undefined): void { | ||
this._setCounter = value; | ||
} | ||
|
||
public static update(): void { | ||
// Increment the counter, so the state change in React and force a redraw each time the active tool changes | ||
// The reason for solution it that I only want to store the active tool at one single location, since this gives a more | ||
// stable code, and never goes out of sync. | ||
// React get the active tool by: renderTarget.commandsController.activeTool; | ||
if (this._setCounter === undefined) { | ||
return; | ||
} | ||
this._counter++; | ||
this._setCounter(this._counter); | ||
} | ||
} |
This file contains 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
24 changes: 24 additions & 0 deletions
24
react-components/src/architecture/base/renderTarget/ContextMenuController.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type AnyIntersection } from '@cognite/reveal'; | ||
import { ContextMenuUpdater } from '../reactUpdaters/ContextMenuUpdater'; | ||
import { type Vector2 } from 'three'; | ||
|
||
export type ContextMenuData = { | ||
position: Vector2; | ||
intersection: AnyIntersection | undefined; | ||
}; | ||
|
||
export class ContextMenuController { | ||
private _contextMenuPosition: ContextMenuData | undefined = undefined; | ||
|
||
public get contextMenuPositionData(): ContextMenuData | undefined { | ||
return this._contextMenuPosition; | ||
} | ||
|
||
public set contextMenuPositionData(data: ContextMenuData | undefined) { | ||
this._contextMenuPosition = data; | ||
ContextMenuUpdater.update(); | ||
} | ||
} |
This file contains 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 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
45 changes: 45 additions & 0 deletions
45
react-components/src/components/Architecture/AnchoredDialog.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type ReactNode, useMemo, useState } from 'react'; | ||
import { useRenderTarget } from '../RevealCanvas'; | ||
import { AnchoredDialogUpdater } from '../../architecture/base/reactUpdaters/AnchoredDialogUpdater'; | ||
import { ViewerAnchor } from '../ViewerAnchor/ViewerAnchor'; | ||
import { Menu } from '@cognite/cogs.js'; | ||
import { createButton } from './CommandButtons'; | ||
import styled from 'styled-components'; | ||
import { withSuppressRevealEvents } from '../../higher-order-components/withSuppressRevealEvents'; | ||
|
||
export const AnchoredDialog = (): ReactNode => { | ||
const [activeToolUpdate, setActiveToolUpdater] = useState<number>(0); | ||
|
||
AnchoredDialogUpdater.setCounterDelegate(setActiveToolUpdater); | ||
|
||
const renderTarget = useRenderTarget(); | ||
if (renderTarget === undefined) { | ||
return <></>; | ||
} | ||
const activeTool = renderTarget.commandsController.activeTool; | ||
if (activeTool === undefined) { | ||
return <></>; | ||
} | ||
|
||
const dialogContent = useMemo(() => activeTool.getAnchoredDialogContent(), [activeToolUpdate]); | ||
const isSomeEnabled = dialogContent?.contentCommands.some((command) => command.isEnabled); | ||
|
||
if (dialogContent === undefined || isSomeEnabled !== true) { | ||
return undefined; | ||
} | ||
|
||
return ( | ||
<ViewerAnchor position={dialogContent?.position}> | ||
<SuppressedMenu> | ||
{dialogContent.contentCommands.map((command) => createButton(command, 'right'))} | ||
</SuppressedMenu> | ||
</ViewerAnchor> | ||
); | ||
}; | ||
|
||
const SuppressedMenu = withSuppressRevealEvents(styled(Menu)` | ||
background-color: white; | ||
`); |
This file contains 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.