-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
chore: move non-shared code from frontend-shared to app #24674
Changes from 8 commits
21736c5
ac3d554
c1808a5
f94ba64
9171926
1fd79c8
d9401c4
90e14b2
edd606b
19b00e6
22f6735
0fc7e6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,12 +183,11 @@ import { gql, useSubscription } from '@urql/vue' | |
import { computed, ref, toRef, watch } from 'vue' | ||
import { Specs_SpecsListFragment, SpecsList_GitInfoUpdatedDocument, SpecsListFragment } from '../generated/graphql' | ||
import { useI18n } from '@cy/i18n' | ||
import { buildSpecTree, fuzzySortSpecs, makeFuzzyFoundSpec, useCachedSpecs } from './spec-utils' | ||
import type { FuzzyFoundSpec } from './spec-utils' | ||
import { useCollapsibleTree } from '@packages/frontend-shared/src/composables/useCollapsibleTree' | ||
import { buildSpecTree, FuzzyFoundSpec, useCollapsibleTree } from './tree/useCollapsibleTree' | ||
import { fuzzySortSpecs, makeFuzzyFoundSpec, useCachedSpecs } from './spec-utils' | ||
import RowDirectory from './RowDirectory.vue' | ||
import SpecItem from './SpecItem.vue' | ||
import { useVirtualList } from '@packages/frontend-shared/src/composables/useVirtualList' | ||
import { useVirtualList } from './tree/useVirtualList' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this has been backported to VueUse, do we want to try using the VueUse implementation: https://vueuse.org/core/usevirtuallist/#usevirtuallist I wonder if we have diverged or if this will just work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, we should do this. Not right here, though -- I don't want to introduce any risk or change to code in this PR where possible - this is a quick reshuffling of things. |
||
import NoResults from '@cy/components/NoResults.vue' | ||
import SpecPatternModal from '../components/SpecPatternModal.vue' | ||
import { useOnline, useResizeObserver } from '@vueuse/core' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,127 +2,7 @@ import fuzzySort from 'fuzzysort' | |
import type { FoundSpec } from '@packages/types' | ||
import { ComputedRef, Ref, ref, watch } from 'vue' | ||
import _ from 'lodash' | ||
import { getRunnerConfigFromWindow } from '../runner' | ||
|
||
// Platform may not be available when this file is loaded (e.g. during component our component tests) so | ||
// we defer loading it until it's used | ||
let platform | ||
const getPlatform = (): string => { | ||
if (!platform) { | ||
platform = getRunnerConfigFromWindow().platform | ||
} | ||
|
||
return platform | ||
} | ||
const getRegexSeparator = () => getPlatform() === 'win32' ? /\\/ : /\// | ||
const getSeparator = () => getPlatform() === 'win32' ? '\\' : '/' | ||
|
||
export type FuzzyFoundSpec<T = FoundSpec> = T & { | ||
fuzzyIndexes: { | ||
relative: number[] | ||
baseName: number[] | ||
} | ||
} | ||
|
||
export type SpecTreeNode<T extends FoundSpec = FoundSpec> = { | ||
id: string | ||
name: string | ||
children: SpecTreeNode<T>[] | ||
isLeaf: boolean | ||
parent?: SpecTreeNode<T> | ||
data?: T | ||
highlightIndexes: number[] | ||
} | ||
|
||
export function buildSpecTree<T extends FoundSpec> (specs: FoundSpec[], root: SpecTreeNode<T> = { name: '', isLeaf: false, children: [], id: '', highlightIndexes: [] }): SpecTreeNode<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
specs.forEach((spec) => buildSpecTreeRecursive(spec.relative, root, spec)) | ||
collapseEmptyChildren(root) | ||
|
||
return root | ||
} | ||
|
||
export function buildSpecTreeRecursive<T extends FoundSpec> (path: string, tree: SpecTreeNode<T>, data?: T) { | ||
const [firstFile, ...rest] = path.split(getRegexSeparator()) | ||
const id = tree.id ? [tree.id, firstFile].join(getSeparator()) : firstFile | ||
|
||
const newNode: SpecTreeNode<T> = { name: firstFile, isLeaf: false, children: [], parent: tree, data, id, highlightIndexes: [] } | ||
|
||
if (rest.length < 1) { | ||
newNode.isLeaf = true | ||
newNode.highlightIndexes = getHighlightIndexes(newNode) | ||
|
||
tree.children.push(newNode) | ||
|
||
return tree | ||
} | ||
|
||
const foundChild = tree.children.find((child) => child.name === firstFile) | ||
|
||
if (foundChild) { | ||
buildSpecTreeRecursive(rest.join(getSeparator()), foundChild, data) | ||
|
||
return tree | ||
} | ||
|
||
newNode.highlightIndexes = getHighlightIndexes(newNode) | ||
const newTree = buildSpecTreeRecursive(rest.join(getSeparator()), newNode, data) | ||
|
||
tree.children.push(newTree) | ||
|
||
return tree | ||
} | ||
|
||
function collapseEmptyChildren<T extends FoundSpec> (node: SpecTreeNode<T>) { | ||
for (const child of node.children) { | ||
collapseEmptyChildren(child) | ||
} | ||
if (node.isLeaf) { | ||
return | ||
} | ||
|
||
// Root name of our tree is '/'. We don't want to collapse into the root node | ||
// so we check node.parent.parent | ||
if (node.parent && node.parent.parent && (node.parent.children.length === 1)) { | ||
node.parent.name = [node.parent.name, node.name].join(getSeparator()) | ||
node.parent.id = [node.parent.id, node.name].join(getSeparator()) | ||
node.parent.children = node.children | ||
node.parent.highlightIndexes = getHighlightIndexes(node.parent) | ||
} | ||
|
||
return | ||
} | ||
|
||
// Given a node, return the indexes of the characters that should be highlighted | ||
// The indexes are matched to the `relative` and `baseName` keys of FoundSpec, and | ||
// depending on whether the node is a leaf or not, the indexes are normalized to the position | ||
// of the node's location in the tree. | ||
// If a search of "src/comp" is given with indexes [0,1,2,3,4,5,6,7], the indexes will be split | ||
// into [0,1,2] and [4,5,6,7] corresponding with a | ||
// - src | ||
// - components | ||
// - index.ts | ||
// tree (given that src/comp is not collapsed) | ||
function getHighlightIndexes <T extends FoundSpec> (node: SpecTreeNode<T>) { | ||
if (!(node.data as any)?.fuzzyIndexes) { | ||
return [] | ||
} | ||
|
||
const { relative: relativeIndexes, baseName: baseNameIndexes } = (node.data as FuzzyFoundSpec<T>).fuzzyIndexes | ||
|
||
// When there is a full (or close to) relative match, baseName will no longer match. | ||
// If we have indexes for baseName we show them, otherwise we pull from relative to derive | ||
// baseName indexes | ||
if (node.isLeaf && baseNameIndexes.length > 0) { | ||
return baseNameIndexes | ||
} | ||
|
||
const maxIndex = node.id.length - 1 | ||
const minIndex = maxIndex - node.name.length + 1 | ||
|
||
const res = relativeIndexes.filter((index) => index >= minIndex && index <= maxIndex) | ||
|
||
return res.map((idx) => idx - minIndex) | ||
} | ||
import { FuzzyFoundSpec, getPlatform } from './tree/useCollapsibleTree' | ||
|
||
export function fuzzySortSpecs <T extends FuzzyFoundSpec> (specs: T[], searchValue: string) { | ||
const normalizedSearchValue = getPlatform() === 'win32' ? searchValue.replaceAll('/', '\\') : searchValue | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused, this was not doing anything but saving the specFilter in the store for no reason.