/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import * as React from "react"; import { useContext, useMemo, useRef, useState } from "react"; import { BeeGwtService, BoxedExpression, DmnDataType, ExpressionChangedArgs, Normalized, PmmlDocument } from "./api"; import { BoxedExpressionEditorProps, OnRequestFeelIdentifiers } from "./BoxedExpressionEditor"; import "./BoxedExpressionEditorContext.css"; export interface BoxedExpressionEditorContextType { // Plumbing beeGwtService?: BeeGwtService; editorRef: React.RefObject; scrollableParentRef: React.RefObject; // Props expressionHolderId: string; pmmlDocuments?: PmmlDocument[]; dataTypes: DmnDataType[]; isReadOnly?: boolean; evaluationHitsCountById?: Map; // State currentlyOpenContextMenu: string | undefined; setCurrentlyOpenContextMenu: React.Dispatch>; onRequestFeelIdentifiers?: OnRequestFeelIdentifiers; widthsById: Map; hideDmn14BoxedExpressions?: boolean; } export interface BoxedExpressionEditorDispatchContextType { setExpression: OnExpressionChange; setWidthsById: (mutation: ({ newMap }: { newMap: Map }) => void) => void; } export const BoxedExpressionEditorContext = React.createContext( {} as BoxedExpressionEditorContextType ); export const BoxedExpressionEditorDispatchContext = React.createContext( {} as BoxedExpressionEditorDispatchContextType ); export function useBoxedExpressionEditor() { return useContext(BoxedExpressionEditorContext); } export function useBoxedExpressionEditorDispatch() { return useContext(BoxedExpressionEditorDispatchContext); } export function BoxedExpressionEditorContextProvider({ onExpressionChange, onWidthsChange, dataTypes, isReadOnly, expressionHolderId, beeGwtService, children, pmmlDocuments, evaluationHitsCountById, scrollableParentRef, onRequestFeelIdentifiers, widthsById, hideDmn14BoxedExpressions, }: React.PropsWithChildren) { const [currentlyOpenContextMenu, setCurrentlyOpenContextMenu] = useState(undefined); const editorRef = useRef(null); const widthsByIdRef = useRef>(widthsById); React.useEffect(() => { widthsByIdRef.current = widthsById; }, [widthsById]); const dispatch = useMemo( () => ({ setExpression: onExpressionChange, setWidthsById: (mutation) => { const newWidthsById = new Map(widthsByIdRef.current); widthsByIdRef.current = newWidthsById; mutation({ newMap: newWidthsById }); onWidthsChange(newWidthsById); }, }), [onExpressionChange, onWidthsChange] ); return (
{children}
); } export type OnSetExpression = (args: { getNewExpression: (prev: Normalized | undefined) => Normalized | undefined; expressionChangedArgs: ExpressionChangedArgs; }) => void; export type OnExpressionChange = (args: { setExpressionAction: React.SetStateAction | undefined>; expressionChangedArgs: ExpressionChangedArgs; }) => void; export function NestedExpressionDispatchContextProvider({ onSetExpression, children, }: React.PropsWithChildren<{ onSetExpression: OnSetExpression; }>) { const { setWidthsById } = useBoxedExpressionEditorDispatch(); const nestedExpressionDispatch = useMemo(() => { return { setExpression: (OnExpressionChange) => { function getNewExpression(prev: Normalized) { return typeof OnExpressionChange.setExpressionAction === "function" ? (OnExpressionChange.setExpressionAction(prev)! as Normalized) : OnExpressionChange.setExpressionAction; } onSetExpression({ getNewExpression, expressionChangedArgs: OnExpressionChange.expressionChangedArgs }); }, setWidthsById, }; }, [onSetExpression, setWidthsById]); return ( {children} ); }