UNPKG

@finos/legend-application-pure-ide

Version:
135 lines 5.24 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed 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 { deserialize } from 'serializr'; import { TreeState } from './TreeState.js'; import { DirectoryNode, } from '../server/models/DirectoryTree.js'; import { action, flow, flowResult, makeObservable, observable } from 'mobx'; import { ACTIVITY_MODE } from './PureIDEConfig.js'; import { assertTrue, guaranteeNonNullable, } from '@finos/legend-shared'; import { DIRECTORY_PATH_DELIMITER } from '@finos/legend-graph'; const getParentPath = (path) => { const trimmedPath = path.trim(); const idx = trimmedPath.lastIndexOf(DIRECTORY_PATH_DELIMITER); if (idx <= 0) { return undefined; } return trimmedPath.substring(0, idx); }; const pathToId = (path) => `file_${path}`; export class DirectoryTreeState extends TreeState { nodeForCreateNewFile; nodeForCreateNewDirectory; nodeForRenameFile; constructor(ideStore) { super(ideStore); makeObservable(this, { nodeForCreateNewFile: observable, nodeForCreateNewDirectory: observable, nodeForRenameFile: observable, setNodeForCreateNewFile: action, setNodeForCreateNewDirectory: action, setNodeForRenameFile: action, revealPath: flow, }); } setNodeForCreateNewFile = (value) => { assertTrue(!value || value.data.isFolderNode, 'Node selected for creating a new file from must be a directory'); this.nodeForCreateNewFile = value; }; setNodeForCreateNewDirectory = (value) => { assertTrue(!value || value.data.isFolderNode, 'Node selected for creating a new directory from must be a directory'); this.nodeForCreateNewDirectory = value; }; setNodeForRenameFile = (value) => { assertTrue(!value || value.data.isFileNode, 'Node selected for renaming must be a file'); this.nodeForRenameFile = value; }; async getRootNodes() { return (await this.ideStore.client.getDirectoryChildren()).map((node) => deserialize(DirectoryNode, node)); } buildTreeData(rootNodes) { const rootIds = []; const nodes = new Map(); rootNodes.forEach((node) => { const id = node.li_attr.id; rootIds.push(id); nodes.set(id, { data: node, id, label: node.text, isLoading: false, }); }); return { rootIds, nodes }; } async getChildNodes(node) { return (await this.ideStore.client.getDirectoryChildren(node.data.li_attr.path)).map((child) => deserialize(DirectoryNode, child)); } processChildNodes(node, childNodes) { const treeData = this.getTreeData(); const childrenIds = []; childNodes.forEach((childNode) => { const id = childNode.li_attr.id; childrenIds.push(id); treeData.nodes.set(id, { data: childNode, id, label: childNode.text, isLoading: false, }); }); node.childrenIds = childrenIds; } *openNode(node) { if (node.data.isFileNode) { yield flowResult(this.ideStore.loadFile(node.data.li_attr.path)); } } *revealPath(path, options) { if (options?.forceOpenExplorerPanel) { this.ideStore.setActiveActivity(ACTIVITY_MODE.FILE_EXPLORER, { keepShowingIfMatchedCurrent: true, }); } const paths = []; let currentPath = path; while (currentPath) { paths.unshift(currentPath); currentPath = getParentPath(currentPath); } for (const _path of paths) { const node = guaranteeNonNullable(this.getTreeData().nodes.get(pathToId(_path)), `Can't find node with path '${_path}'`); if (node.data.isFolderNode) { yield flowResult(this.expandNode(node)); } else { if (options?.directoryOnly) { throw new Error(`Can't reveal non-directory path`); } else { yield flowResult(this.ideStore.loadFile(_path, options?.coordinate)); } } } const currentNode = guaranteeNonNullable(this.getTreeData().nodes.get(pathToId(path)), `Can't find node with path '${path}'`); this.setSelectedNode(currentNode); document.getElementById(currentNode.id)?.scrollIntoView({ behavior: 'instant', block: 'center', }); } } //# sourceMappingURL=DirectoryTreeState.js.map