Skip to content

Commit

Permalink
Making a few UI tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Cake committed Aug 8, 2024
1 parent d730486 commit be070f2
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 15 deletions.
137 changes: 136 additions & 1 deletion .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"dependencies": {
"@j-cake/jcake-utils": "latest",
"chalk": "latest",
"expressions": "link:../expressions",
"lucide-react": "^0.424.0",
"react": "latest",
"react-dom": "latest",
"expressions": "link:../expressions"
"react-dom": "latest"
},
"scripts": {
"build:plugin": "esbuild src/main.ts --outdir=build --bundle --sourcemap --platform=node --format=cjs --external:obsidian --external:electron",
Expand All @@ -24,6 +25,7 @@
"@j-cake/mkjson": "latest",
"@types/node": "latest",
"@types/react": "latest",
"@types/react-dom": "^18.3.0",
"electron": "latest",
"esbuild": "latest",
"obsidian": "latest",
Expand Down
12 changes: 9 additions & 3 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const typeDef: Record<string, Type<any>> = {
export default class DataSource<FM extends Partial<FrontMatter>> {
private onChange: () => void = () => void 0;

private raw: string[][] = [];
private raw: string[][] = [[""]];

// If not specified, assume the following defaults.
public frontMatter: FM = { urlEscaped: true } as FM;
Expand Down Expand Up @@ -161,8 +161,14 @@ export default class DataSource<FM extends Partial<FrontMatter>> {
}
}

public addRow() {
this.data.push(new Array(this.columnNames.length).fill(""));
public insertColumn() {
this.columnNames.push(`col ${this.columnNames.length + 1}`);
this.raw.forEach(i => i.push(""));
this.onChange();
}

public insertRow() {
this.raw.push(new Array(this.columnNames.length).fill(""));
this.onChange();
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export default class Range {
public constructor(public from: Cell, public to: Cell) {
}

static fromVector(vector: Vector, end: Cell): Range {
// TODO: Implement
}

public eq(b: Range): boolean {
return this.from.eq(b.from) && this.to.eq(b.to);
}
Expand Down Expand Up @@ -79,4 +83,14 @@ export class Cell {
this.col = Math.max(0, this.col + amount);
return this;
}
}

export class Vector extends Range {
static fromRow(row: number): Vector {

}

static fromCol(col: number): Vector {

}
}
61 changes: 52 additions & 9 deletions src/table.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import * as icon from 'lucide-react';
import * as obs from 'obsidian';

import DataSource, {DEFAULT_COLUMN_WIDTH, DEFAULT_ROW_HEIGHT, MIN_COLUMN_WIDTH, MIN_ROW_HEIGHT} from "./data.js";
import Range, {Cell} from "./range.js";
import Range, {Cell, Vector} from "./range.js";
import DataVisualiser from "./visualiser.js";

interface TableProps {
Expand All @@ -15,7 +17,7 @@ type State<T> = {

export type SelectionState = {
selected: Range[],
dragStart: Cell | null,
dragStart: Cell | Vector | null,
cell: Cell | null,
hoverCell: Cell | null
};
Expand Down Expand Up @@ -137,7 +139,8 @@ export default function Table({data}: TableProps) {
style={{
gridColumn: col + 2,
gridRow: 1
}}>
}}
onContextMenu={e => headerContextMenu(e)}>
<div className={"column-title"}>
{column} {data.frontMatter?.columnTypes?.[col] ?
<div className={"nav-file-tag"}>{data.frontMatter?.columnTypes?.[col]}</div> : null}
Expand All @@ -148,7 +151,10 @@ export default function Table({data}: TableProps) {
x: e.clientX,
y: e.clientY
},
prevSize: {width: data.columnWidths[col] ?? DEFAULT_COLUMN_WIDTH, height: e.currentTarget.innerHeight},
prevSize: {
width: data.columnWidths[col] ?? DEFAULT_COLUMN_WIDTH,
height: e.currentTarget.innerHeight
},
onResize: size => data.columnWidths[col] = Math.max(size.width, MIN_COLUMN_WIDTH)
})}/>
</div>)}
Expand All @@ -159,8 +165,9 @@ export default function Table({data}: TableProps) {
style={{
gridColumn: 1,
gridRow: row + 2
}}>
<div className={"row-title"}>{row}</div>
}}
onContextMenu={e => headerContextMenu(e)}>
<div className={"row-title"}>{row + 1}</div>
<span
className={"resize-handle horizontal"}
onMouseDown={e => setResize({
Expand All @@ -169,7 +176,10 @@ export default function Table({data}: TableProps) {
x: e.clientX,
y: e.clientY
},
prevSize: {height: data.rowHeights[row] ?? DEFAULT_COLUMN_WIDTH, width: e.currentTarget.innerWidth},
prevSize: {
height: data.rowHeights[row] ?? DEFAULT_COLUMN_WIDTH,
width: e.currentTarget.innerWidth
},
onResize: size => data.rowHeights[row] = Math.max(size.height, MIN_ROW_HEIGHT)
})}/>
</div>)}
Expand Down Expand Up @@ -199,9 +209,30 @@ export default function Table({data}: TableProps) {
<DataVisualiser data={data.valueAt(new Cell(row, col))}/>
</div>))}

<div
className={"add-btn"}
style={{
gridRow: "1 / -1",
gridColumn: data.columnNames.length + 2
}}
tabIndex={0}
onClick={e => data.insertColumn()}>
<icon.BetweenVerticalStart size={16}/>
</div>
<div
className={"add-btn"}
style={{
gridRow: data.data.length + 2,
gridColumn: "1 / -1"
}}
tabIndex={0}
onClick={e => data.insertRow()}>
<icon.BetweenHorizontalStart size={16}/>
</div>

<Selection
ranges={selected.selected}
current={selected.dragStart && selected.hoverCell ? new Range(selected.dragStart, selected.hoverCell) : null}
current={selected.dragStart && selected.hoverCell ? selected.dragStart instanceof Cell ? new Range(selected.dragStart, selected.hoverCell) : Range.fromVector(selected.dragStart, selected.hoverCell) : null}
tableBodyRef={references.tbody}
getRef={cell => getCell(cell)?.current?.getBoundingClientRect()!}/>
</div>
Expand Down Expand Up @@ -236,7 +267,7 @@ export function Selection(props: {
}

export function finishSelection(e: React.MouseEvent, cell: Cell, selection: State<SelectionState>) {
const range = new Range(selection.state.dragStart ?? cell, cell);
const range = selection.state.dragStart instanceof Vector ? Range.fromVector(selection.state.dragStart, cell) : new Range(selection.state.dragStart ?? cell, cell);

let newSelection: Range[] = [];

Expand Down Expand Up @@ -278,3 +309,15 @@ export function getActiveCell(selected: Range[]): Cell | null {
return selected[0].topLeft;
else return null
}

export function headerContextMenu(e: React.MouseEvent) {
const menu = new obs.Menu();

menu.addSeparator();
menu.addItem(item => item
.setIcon("trash-2")
.setTitle("Delete")
.onClick(e => {}));

menu.showAtMouseEvent(e.nativeEvent);
}
5 changes: 5 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,9 @@ thead td {

.selection-range.in-progress {
border-color: var(--text-accent);
}

.add-btn {
display: flex;
justify-content: center;
}

0 comments on commit be070f2

Please sign in to comment.