add graph projection demo for spreadsheets

This commit is contained in:
“chrisshank” 2024-11-15 15:15:30 -08:00
parent 49ef97508d
commit 3b6975b9db
5 changed files with 231 additions and 7 deletions

View File

@ -29,9 +29,7 @@
</head>
<body>
<fc-geometry id="box1" x="100" y="100" width="50" height="50"></fc-geometry>
<fc-geometry id="box2" x="200" y="300" width="50" height="50"
>Hello World</fc-geometry
>
<fc-geometry id="box2" x="200" y="300">Hello World</fc-geometry>
<event-propagator
source="#box1"
target="#box2"

View File

@ -27,8 +27,10 @@
<li><a href="ink.html">Ink</a></li>
<li><a href="maps.html">Maps</a></li>
<li><a href="music.html">Music</a></li>
<li><a href="perf.html">Perf</a></li>
<li><a href="shapes.html">Shapes</a></li>
<li><a href="spreadsheet.html">Spreadsheet</a></li>
<li><a href="spreadsheet-graph.html">Spreadsheet Graph</a></li>
<!-- <li><a href="chains-of-thought/index.html">Chains of thought</a></li> -->
</ul>
</body>

219
demo/spreadsheet-graph.html Normal file
View File

@ -0,0 +1,219 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Spreadsheet Graph</title>
<style>
html {
height: 100%;
}
body {
min-height: 100%;
position: relative;
margin: 0;
}
fc-connection {
display: block;
position: absolute;
inset: 0 0 0 0;
z-index: calc(infinity);
}
body[overlay] {
s-table::after {
display: block;
content: '';
position: fixed;
inset: 0 0 0 0;
z-index: 4;
background-color: rgba(0, 0, 0, 0.25);
}
s-cell[graph] {
position: fixed;
top: var(--cell-top);
left: var(--cell-left);
z-index: 10;
width: var(--cell-width);
height: var(--cell-height);
}
}
</style>
</head>
<body>
<button>Toggle</button>
<fc-geometry x="50" y="50" width="500">
<s-table>
<s-cell
column="A"
row="1"
expression="1"
style="--cell-top: 100px; --cell-left: 100px; view-transition-name: A1"
></s-cell>
<s-cell column="B" row="1"></s-cell>
<s-cell column="C" row="1"></s-cell>
<s-cell column="D" row="1"></s-cell>
<s-cell column="E" row="1"></s-cell>
<s-cell column="F" row="1"></s-cell>
<s-cell column="G" row="1"></s-cell>
<s-cell column="H" row="1"></s-cell>
<s-cell column="I" row="1"></s-cell>
<s-cell column="J" row="1"></s-cell>
<s-cell
column="A"
row="2"
expression="$A1 * 2"
style="--cell-top: 200px; --cell-left: 100px; view-transition-name: A2"
></s-cell>
<s-cell column="B" row="2"></s-cell>
<s-cell column="C" row="2"></s-cell>
<s-cell column="D" row="2"></s-cell>
<s-cell column="E" row="2"></s-cell>
<s-cell column="F" row="2"></s-cell>
<s-cell column="G" row="2"></s-cell>
<s-cell column="H" row="2"></s-cell>
<s-cell column="I" row="2"></s-cell>
<s-cell column="J" row="2"></s-cell>
<s-cell column="A" row="3"></s-cell>
<s-cell column="B" row="3"></s-cell>
<s-cell column="C" row="3"></s-cell>
<s-cell column="D" row="3"></s-cell>
<s-cell column="E" row="3"></s-cell>
<s-cell column="F" row="3"></s-cell>
<s-cell column="G" row="3"></s-cell>
<s-cell column="H" row="3"></s-cell>
<s-cell column="I" row="3"></s-cell>
<s-cell column="J" row="3"></s-cell>
<s-cell column="A" row="4"></s-cell>
<s-cell column="B" row="4"></s-cell>
<s-cell
column="C"
row="4"
expression="$A1 + $A2"
style="--cell-top: 150px; --cell-left: 250px; view-transition-name: C4"
></s-cell>
<s-cell column="D" row="4"></s-cell>
<s-cell column="E" row="4"></s-cell>
<s-cell column="F" row="4"></s-cell>
<s-cell column="G" row="4"></s-cell>
<s-cell column="H" row="4"></s-cell>
<s-cell column="I" row="4"></s-cell>
<s-cell column="J" row="4"></s-cell>
<s-cell column="A" row="5"></s-cell>
<s-cell column="B" row="5"></s-cell>
<s-cell column="C" row="5"></s-cell>
<s-cell column="D" row="5"></s-cell>
<s-cell column="E" row="5"></s-cell>
<s-cell column="F" row="5"></s-cell>
<s-cell column="G" row="5"></s-cell>
<s-cell column="H" row="5"></s-cell>
<s-cell column="I" row="5"></s-cell>
<s-cell column="J" row="5"></s-cell>
<s-cell column="A" row="6"></s-cell>
<s-cell column="B" row="6"></s-cell>
<s-cell column="C" row="6"></s-cell>
<s-cell column="D" row="6"></s-cell>
<s-cell column="E" row="6"></s-cell>
<s-cell column="F" row="6"></s-cell>
<s-cell column="G" row="6"></s-cell>
<s-cell column="H" row="6"></s-cell>
<s-cell column="I" row="6"></s-cell>
<s-cell column="J" row="6"></s-cell>
<s-cell column="A" row="7"></s-cell>
<s-cell column="B" row="7"></s-cell>
<s-cell column="C" row="7"></s-cell>
<s-cell column="D" row="7"></s-cell>
<s-cell column="E" row="7"></s-cell>
<s-cell column="F" row="7"></s-cell>
<s-cell column="G" row="7"></s-cell>
<s-cell column="H" row="7"></s-cell>
<s-cell column="I" row="7"></s-cell>
<s-cell column="J" row="7"></s-cell>
<s-cell column="A" row="8"></s-cell>
<s-cell column="B" row="8"></s-cell>
<s-cell column="C" row="8"></s-cell>
<s-cell column="D" row="8"></s-cell>
<s-cell column="E" row="8"></s-cell>
<s-cell column="F" row="8"></s-cell>
<s-cell column="G" row="8"></s-cell>
<s-cell column="H" row="8"></s-cell>
<s-cell column="I" row="8"></s-cell>
<s-cell column="J" row="8"></s-cell>
<s-cell column="A" row="9"></s-cell>
<s-cell column="B" row="9"></s-cell>
<s-cell column="C" row="9"></s-cell>
<s-cell column="D" row="9"></s-cell>
<s-cell column="E" row="9"></s-cell>
<s-cell column="F" row="9"></s-cell>
<s-cell column="G" row="9"></s-cell>
<s-cell column="H" row="9"></s-cell>
<s-cell column="I" row="9"></s-cell>
<s-cell column="J" row="9"></s-cell>
<s-cell column="A" row="10"></s-cell>
<s-cell column="B" row="10"></s-cell>
<s-cell column="C" row="10"></s-cell>
<s-cell column="D" row="10"></s-cell>
<s-cell column="E" row="10"></s-cell>
<s-cell column="F" row="10"></s-cell>
<s-cell column="G" row="10"></s-cell>
<s-cell column="H" row="10"></s-cell>
<s-cell column="I" row="10"></s-cell>
<s-cell column="J" row="10"></s-cell>
</s-table>
</fc-geometry>
<script type="module">
import { FolkGeometry } from '../src/canvas/fc-geometry.ts';
import { FolkConnection } from '../src/arrows/fc-connection.ts';
import { SpreadsheetTable } from '../src/spreadsheet/spreadsheet.ts';
FolkGeometry.register();
FolkConnection.register();
SpreadsheetTable.register();
let isProjected = false;
const button = document.querySelector('button');
button.addEventListener('click', () => {
isProjected = !isProjected;
document.startViewTransition(() => {
isProjected ? addOverlayGraph() : removeOverlayGraph();
});
});
function addOverlayGraph() {
document.body.setAttribute('overlay', '');
document.querySelectorAll('s-cell').forEach((cell) => {
if (cell.dependencies.length === 0) return;
cell.setAttribute('graph', '');
for (const dep of cell.dependencies) {
dep.setAttribute('graph', '');
const connection = document.createElement('fc-connection');
connection.source = `s-cell[row="${dep.row}"][column="${dep.column}"]`;
connection.target = `s-cell[row="${cell.row}"][column="${cell.column}"]`;
document.body.appendChild(connection);
}
});
}
function removeOverlayGraph() {
document.body.removeAttribute('overlay', '');
document.querySelectorAll('fc-connection').forEach((arrow) => arrow.remove());
document.querySelectorAll('s-cell').forEach((cell) => {
cell.removeAttribute('graph', '');
});
}
</script>
</body>
</html>

View File

@ -2,7 +2,7 @@
"tasks": {
"dev": "vite demo",
"build": "vite build demo --base=/folk-canvas",
"preview": "deno run build && vite preview demo"
"preview": "vite build demo && vite preview demo"
},
"imports": {
"perfect-arrows": "npm:perfect-arrows@^0.3.7",

View File

@ -24,6 +24,7 @@ textarea {
background-color: rgba(255, 255, 255, 0.75);
grid-column: var(--text-column, 0);
grid-row: var(--text-row, 0);
z-index: 11;
}
s-columns {
@ -105,7 +106,7 @@ s-columns, s-rows, s-body {
::slotted(s-cell:focus) {
outline: 2px solid #1b73e8;
z-index: 2;
z-index: 4;
}
`);
@ -151,9 +152,9 @@ export class SpreadsheetTable extends HTMLElement {
static tagName = 's-table';
static register() {
customElements.define(this.tagName, this);
SpreadsheetCell.register();
SpreadsheetHeader.register();
customElements.define(this.tagName, this);
}
#shadow = this.attachShadow({ mode: 'open' });
@ -225,6 +226,10 @@ export class SpreadsheetTable extends HTMLElement {
this.#range = range;
}
getCell(column: string, row: number | string): SpreadsheetCell | null {
return this.querySelector(`s-cell[column="${column}"][row="${row}"]`);
}
handleEvent(event: Event) {
switch (event.type) {
case 'keydown': {
@ -454,7 +459,7 @@ export class SpreadsheetCell extends HTMLElement {
}
#getCell(column: string, row: number | string): SpreadsheetCell | null {
return document.querySelector(`s-cell[column="${column}"][row="${row}"]`);
return this.parentElement!.querySelector(`s-cell[column="${column}"][row="${row}"]`);
}
get cellAbove() {