Skip to content

Commit

Permalink
Allow change of expansion icons and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed May 14, 2024
1 parent de40e9c commit a8f7144
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
4 changes: 0 additions & 4 deletions src/assets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,3 @@ th.auto-width {
.cursor-help {
cursor: help;
}

.expansion-button {
all: unset;
}
12 changes: 7 additions & 5 deletions src/lib/Branch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@
on:dragend={(e) => handleDragEnd(e, node)}
>
{#if hasChildren}
<button class="expansion-button" on:click={() => setExpansion(node, !expanded)}>
<i
class="far {expanded ? classes.expandedToggleClass : classes.collapsedToggleClass}"
/>
<button
class="expansion-button"
on:click={() => setExpansion(node, !expanded)}
type="button"
>
<i class="fa-fw {expanded ? classes.expandIcon : classes.collapseIcon}" />
</button>
{:else}
<span />
<span class="fa-fw" />
{/if}

<Checkbox
Expand Down
22 changes: 14 additions & 8 deletions src/lib/TreeView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
* Classes used in tree. You can override default classes with this prop.
* It is recommended to use default classes and add aditinal styles in your css
*/
export let customClasses: CustomizableClasses = defaultClasses;
export let customClasses: Partial<CustomizableClasses> = {};
// use any so use doesnt have to cast from unknown
/**
Expand Down Expand Up @@ -134,6 +134,8 @@
let validTarget = false;
let insPos: InsertionType;
$: computedClasses = { ...defaultClasses, ...(customClasses ?? {}) };
$: dragAndDrop && console.warn('Drag and drop is not supported in this version');
$: helper = new TreeHelper({
Expand All @@ -145,8 +147,11 @@
export function changeAllExpansion(changeTo: boolean) {
debugLog('chaning expantion of every node to ', changeTo ? 'expanded' : 'collapsed');
expandedIds = computedTree.map((node) => node.id);
if (changeTo) {
expandedIds = computedTree.map((node) => node.id);
} else {
expandedIds = [];
}
}
function computeTree(
Expand All @@ -163,15 +168,16 @@
return [];
}
let mappedTree = helper.mapTree(userProvidedTree, filter, { ...defaultPropNames, ...props });
const mappedTree = helper.mapTree(userProvidedTree, { ...defaultPropNames, ...props });
const filteredTree = helper.searchTree(mappedTree, filter);
helper.markExpanded(mappedTree, expandedIds);
helper.markExpanded(filteredTree, expandedIds);
// TODO here we could save last value and only recompute visual state if value changed
// or use diff to only update affected nodes
selectionProvider.markSelected(mappedTree, value);
selectionProvider.markSelected(filteredTree, value);
return mappedTree;
return filteredTree;
}
function onExpand(event: CustomEvent<{ node: Node; changeTo: boolean }>) {
Expand Down Expand Up @@ -280,7 +286,7 @@
{highlightedNode}
{readonly}
{helper}
classes={customClasses}
classes={computedClasses}
{verticalLines}
on:open-ctxmenu={openContextMenu}
on:internal-expand={onExpand}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const defaultClasses: CustomizableClasses = {
expandClass: 'inserting-highlighted',
currentlyDraggedClass: 'currently-dragged',
nodeClass: '',
expandedToggleClass: 'fa-plus-square',
collapsedToggleClass: 'fa-minus-square',
expandIcon: 'far fa-fw fa-plus-square',
collapseIcon: 'far fa-fw fa-minus-square',
inserLineClass: '',
inserLineNestClass: ''
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/helpers/tree-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class TreeHelper {
this.config = config;
}

mapTree(tree: Tree, filter: FilterFunction | null, properties: Props): Node[] {
mapTree(tree: Tree, properties: Props): Node[] {
{
return this.searchTree(tree, filter).map((node: any) => {
return tree.map((node: any) => {
// TODO maybe create class for nodes
const mappedNode: Node = {
originalNode: node,
Expand Down
5 changes: 2 additions & 3 deletions src/lib/tree-styles.sass
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ $treeview-lines: solid black 1px
color: #555
font-weight: 700
position: relative
&:not(.has-children)
.tree-item
margin-left: 14px

.tree-item
display: flex
Expand Down Expand Up @@ -103,3 +100,5 @@ $treeview-lines: solid black 1px
color: LightGray
.pointer-cursor
cursor: grab
.expansion-button
all: unset
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export type NodeId = string | number;
export type CustomizableClasses = {
treeClass: string;
nodeClass: string;
expandedToggleClass: string;
collapsedToggleClass: string;
expandIcon: string;
collapseIcon: string;
expandClass: string;
inserLineClass: string;
inserLineNestClass: string;
Expand Down

0 comments on commit a8f7144

Please sign in to comment.