Skip to content
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

#501 #511

Merged
merged 7 commits into from
Dec 14, 2023
Merged

#501 #511

Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/_node/apis.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { doFileActions, doNodeActions } from "./";
import { doNodeActions } from "./node";
import { doFileActions } from "./file";

export const callNodeApi = doNodeActions;
export const callFileApi = doFileActions;
4 changes: 4 additions & 0 deletions src/_redux/main/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const MainContext: Context<TMainContext> = createContext<TMainContext>({
current: false,
},
setIsContentProgrammaticallyChanged: () => {},
isCodeTyping: {
current: false,
},
setIsCodeTyping: () => {},

importProject: () => {},
onUndo: () => {},
Expand Down
11 changes: 9 additions & 2 deletions src/_redux/main/nodeTree/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit";

import { TUpdateTreeViewStatePayload } from "../types";
import { TNodeTreeReducerState } from "./types";
import { TCodeSelection } from "@_components/main/codeView";

const nodeTreeReducerInitialState: TNodeTreeReducerState = {
nodeTree: {},
validNodeTree: {},

needToSelectNodePaths: [],
needToSelectNodePaths: null,
needToSelectCode: null,

nodeTreeViewState: {
focusedItem: "",
Expand All @@ -32,10 +34,14 @@ const nodeTreeSlice = createSlice({
state.validNodeTree = validNodeTree;
},

setNeedToSelectNodePaths(state, action: PayloadAction<string[]>) {
setNeedToSelectNodePaths(state, action: PayloadAction<string[] | null>) {
const needToSelectNodePaths = action.payload;
state.needToSelectNodePaths = needToSelectNodePaths;
},
setNeedToSelectCode(state, action: PayloadAction<TCodeSelection | null>) {
const needToSelectCode = action.payload;
state.needToSelectCode = needToSelectCode;
},

focusNodeTreeNode(state, action: PayloadAction<TNodeUid>) {
const focusedItem = action.payload;
Expand Down Expand Up @@ -146,6 +152,7 @@ export const {
setValidNodeTree,

setNeedToSelectNodePaths,
setNeedToSelectCode,

focusNodeTreeNode,
setExpandedNodeTreeNodes,
Expand Down
4 changes: 3 additions & 1 deletion src/_redux/main/nodeTree/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { TNodeTreeData, TNodeUid } from "@_node/types";

import { TTreeViewState } from "../types";
import { TCodeSelection } from "@_components/main/codeView";

export type TNodeTreeReducerState = {
nodeTree: TNodeTreeData;
validNodeTree: TNodeTreeData;

needToSelectNodePaths: string[];
needToSelectNodePaths: string[] | null;
needToSelectCode: TCodeSelection | null;

nodeTreeViewState: TTreeViewState;
hoveredNodeUid: TNodeUid;
Expand Down
2 changes: 2 additions & 0 deletions src/_redux/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export type TMainContext = {
setIframeRefRef: (iframeRef: HTMLIFrameElement | null) => void;
isContentProgrammaticallyChanged: React.RefObject<boolean>;
setIsContentProgrammaticallyChanged: (value: boolean) => void;
isCodeTyping: React.RefObject<boolean>;
setIsCodeTyping: (value: boolean) => void;

importProject: (
fsType: TProjectContext,
Expand Down
2 changes: 2 additions & 0 deletions src/_redux/useAppState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const useAppState = () => {
nodeTree,
validNodeTree,
needToSelectNodePaths,
needToSelectCode,
nodeTreeViewState: {
focusedItem: nFocusedItem,
expandedItems: nExpandedItems,
Expand Down Expand Up @@ -116,6 +117,7 @@ export const useAppState = () => {
validNodeTree,

needToSelectNodePaths,
needToSelectCode,

nFocusedItem,
nExpandedItems,
Expand Down
14 changes: 8 additions & 6 deletions src/components/main/actionsPanel/nodeTreeView/NodeTreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,14 @@ const NodeTreeView = () => {
}),
[],
);

const searchConfig = {
canSearch: false,
canSearchByStartingTyping: false,
canRename: false,
};
const searchConfig = useMemo(
mrmatthewnguyen105 marked this conversation as resolved.
Show resolved Hide resolved
() => ({
canSearch: false,
canSearchByStartingTyping: false,
canRename: false,
}),
[],
);

return currentFileUid !== "" ? (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { isAddNodeAction, isRenameNodeAction } from "@_node/helpers";
import { MainContext } from "@_redux/main";
import { useAppState } from "@_redux/useAppState";

import { useNodeActionsHandlers } from "./useNodeActionsHandlers";
import { useNodeActionHandlers } from "./useNodeActionHandlers";

export const useCmdk = () => {
const { activePanel, currentCommand } = useAppState();
const {} = useContext(MainContext);

const {
onAddNode,
Expand All @@ -20,7 +19,7 @@ export const useCmdk = () => {
onTurnInto,
onGroup,
onUngroup,
} = useNodeActionsHandlers();
} = useNodeActionHandlers();

useEffect(() => {
if (!currentCommand) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { useCallback, useContext } from "react";
import { useDispatch } from "react-redux";

import { LogAllow } from "@_constants/global";
import { callNodeApi } from "@_node/apis";
import { TNodeUid } from "@_node/types";
import { MainContext } from "@_redux/main";
import { useAppState } from "@_redux/useAppState";
import { doNodeActions } from "@_node/node";
import { callNodeApi } from "@_node/apis";

export const useNodeActionsHandlers = () => {
export const useNodeActionHandlers = () => {
const dispatch = useDispatch();
const {
nodeTree,
Expand Down Expand Up @@ -57,7 +58,6 @@ export const useNodeActionsHandlers = () => {
},
[nodeTree, focusedItem],
);

const onCut = useCallback(() => {
if (selectedItems.length === 0) return;

Expand All @@ -83,7 +83,6 @@ export const useNodeActionsHandlers = () => {
() => setIsContentProgrammaticallyChanged(false),
);
}, [selectedItems, nodeTree]);

const onCopy = useCallback(() => {
if (selectedItems.length === 0) return;

Expand All @@ -109,7 +108,6 @@ export const useNodeActionsHandlers = () => {
() => setIsContentProgrammaticallyChanged(false),
);
}, [selectedItems, nodeTree]);

const onPaste = useCallback(() => {
const focusedNode = validNodeTree[focusedItem];
if (!focusedNode || !focusedNode.data.sourceCodeLocation) {
Expand Down Expand Up @@ -140,7 +138,6 @@ export const useNodeActionsHandlers = () => {
() => setIsContentProgrammaticallyChanged(false),
);
}, [validNodeTree, focusedItem]);

const onDelete = useCallback(() => {
if (selectedItems.length === 0) return;

Expand All @@ -166,7 +163,6 @@ export const useNodeActionsHandlers = () => {
() => setIsContentProgrammaticallyChanged(false),
);
}, [selectedItems, nodeTree]);

const onDuplicate = useCallback(() => {
if (selectedItems.length === 0) return;

Expand All @@ -192,7 +188,6 @@ export const useNodeActionsHandlers = () => {
() => setIsContentProgrammaticallyChanged(false),
);
}, [selectedItems, nodeTree]);

const onMove = useCallback(
({
selectedUids,
Expand Down Expand Up @@ -232,7 +227,6 @@ export const useNodeActionsHandlers = () => {
},
[nodeTree],
);

const onTurnInto = useCallback(
(actionName: string) => {
const focusedNode = nodeTree[focusedItem];
Expand Down Expand Up @@ -264,7 +258,6 @@ export const useNodeActionsHandlers = () => {
},
[nodeTree, focusedItem],
);

const onGroup = useCallback(() => {
if (selectedItems.length === 0) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TNodeUid } from "@_node/types";
import { MainContext } from "@_redux/main";
import { useAppState } from "@_redux/useAppState";

import { useNodeActionsHandlers } from "./useNodeActionsHandlers";
import { useNodeActionHandlers } from "./useNodeActionHandlers";
import { useNodeViewState } from "./useNodeViewState";

export const useNodeTreeCallback = (
Expand All @@ -17,7 +17,7 @@ export const useNodeTreeCallback = (
const { validNodeTree } = useAppState();
const { htmlReferenceData } = useContext(MainContext);

const { onMove } = useNodeActionsHandlers();
const { onMove } = useNodeActionHandlers();
const { cb_focusNode, cb_selectNode, cb_expandNode, cb_collapseNode } =
useNodeViewState();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ export default function WorkspaceTreeView() {
// open default initial html file
useEffect(() => {
if (initialFileUidToOpen !== "" && fileTree[initialFileUidToOpen]) {
addRunningActions([
"fileTreeView-focus",
"fileTreeView-select",
"fileTreeView-read",
]);
addRunningActions(["fileTreeView-read"]);

cb_focusNode(initialFileUidToOpen);
cb_selectNode([initialFileUidToOpen]);
Expand Down
10 changes: 3 additions & 7 deletions src/components/main/codeView/CodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Editor, loader } from "@monaco-editor/react";

import { useCmdk, useEditor } from "./hooks";
import { CodeViewProps } from "./types";
import { getNodeUidByCodeSelection } from "./helpers";

loader.config({ monaco });

Expand All @@ -35,11 +36,7 @@ export default function CodeView(props: CodeViewProps) {
activePanel,
showCodeView,
} = useAppState();
const {
isContentProgrammaticallyChanged,
setIsContentProgrammaticallyChanged,
monacoEditorRef,
} = useContext(MainContext);
const { isCodeTyping, monacoEditorRef } = useContext(MainContext);

const {
handleEditorDidMount,
Expand All @@ -54,7 +51,6 @@ export default function CodeView(props: CodeViewProps) {
setWordWrap,

codeSelection,
getNodeUidByCodeSelection,
} = useEditor();
useCmdk();

Expand Down Expand Up @@ -126,7 +122,7 @@ export default function CodeView(props: CodeViewProps) {

// code select -> selectedUids
useEffect(() => {
if (!codeSelection) return;
if (!codeSelection || isCodeTyping.current) return;

const file = fileTree[currentFileUid];
if (!file) return;
Expand Down
42 changes: 42 additions & 0 deletions src/components/main/codeView/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { TTheme } from "@_redux/global";
import { getSystemTheme } from "@_services/global";
import { TCodeSelection } from "./types";
import { TNodeTreeData, TNodeUid } from "@_node/types";
import { getSubNodeUidsByBfs } from "@_node/helpers";
import { RootNodeUid } from "@_constants/main";

export const getLanguageFromExtension = (extension: string) => {
if (!!extension) return extension;
Expand All @@ -18,3 +22,41 @@ export const getCodeViewTheme = (theme: TTheme) => {
return "light";
}
};

export const getNodeUidByCodeSelection = (
selection: TCodeSelection,
validNodeTree: TNodeTreeData,
): TNodeUid | null => {
let focusedItem: TNodeUid | null = null;
if (selection) {
const uids = getSubNodeUidsByBfs(RootNodeUid, validNodeTree);
uids.reverse();
for (const uid of uids) {
const node = validNodeTree[uid];
const sourceCodeLocation = node.data.sourceCodeLocation;
if (!sourceCodeLocation) continue;

const {
startLine: startLineNumber,
startCol: startColumn,
endCol: endColumn,
endLine: endLineNumber,
} = sourceCodeLocation;

const containFront =
selection.startLineNumber === startLineNumber
? selection.startColumn > startColumn
: selection.startLineNumber > startLineNumber;
const containBack =
selection.endLineNumber === endLineNumber
? selection.endColumn < endColumn
: selection.endLineNumber < endLineNumber;

if (containFront && containBack) {
focusedItem = uid;
break;
}
}
}
return focusedItem;
};
Loading
Loading