Skip to content

Commit

Permalink
More <Flex> progress and other misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 11, 2024
1 parent 9e68cd4 commit 5ed03ad
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 104 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"linter": {
"enabled": false
"enabled": true
},
"javascript": {
"formatter": {
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/use-dimensions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useMemo } from 'react';
import { useParent } from './use-parent.js';

export function useDimensions() {
const root = useParent();
return {
width: root.ctx.canvas.width,
height: root.ctx.canvas.height,
};
const { width, height } = root.ctx.canvas;
return useMemo(() => {
return { width, height };
}, [width, height]);
}
2 changes: 1 addition & 1 deletion src/hooks/use-text-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export function useTextMetrics(
return useMemo(() => {
root.ctx.font = `${fontWeight} ${fontSize}px "${fontFamily}"`;
return root.ctx.measureText(text);
}, [text, fontWeight]);
}, [root, text, fontWeight, fontSize, fontFamily]);
}
10 changes: 2 additions & 8 deletions src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ export class Image extends Entity {
}

async loadImage() {
let img: IImage | undefined;
try {
img = await this.#root.loadImage(this.#src);
} catch (err) {
throw err;
}

const img = await this.#root.loadImage(this.#src);
this.#image = img;
if (this.width === 0) {
this.width = img.naturalWidth;
Expand All @@ -65,7 +59,7 @@ export class Image extends Entity {

render(): void {
super.render();
let { root } = this;
const { root } = this;
const img = this.#image;
if (!img) return;
root.ctx.drawImage(
Expand Down
42 changes: 30 additions & 12 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,26 @@ const reconciler = ReactReconciler<
if (is('Group', t)) {
// @ts-expect-error "root" is missing from the type, but definitely gets passed in from the <Group> component
return new Group(t.props);
} else if (is('Arc', t)) {
}
if (is('Arc', t)) {
return new Arc(t.props);
} else if (is('Canvas', t)) {
}
if (is('Canvas', t)) {
return new Canvas(t.props, root);
} else if (is('Image', t)) {
}
if (is('Image', t)) {
return new Image(t.props, root);
} else if (is('Path', t)) {
}
if (is('Path', t)) {
return new Path(t.props);
} else if (is('Rect', t)) {
}
if (is('Rect', t)) {
return new Rect(t.props);
} else if (is('RoundRect', t)) {
}
if (is('RoundRect', t)) {
return new RoundRect(t.props);
} else if (is('Text', t)) {
}
if (is('Text', t)) {
return new Text({ ...t.props, value: getText(t.props) });
}
throw new Error(`Unsupported type: ${type}`);
Expand Down Expand Up @@ -247,7 +254,7 @@ const reconciler = ReactReconciler<
return null;
},
resetAfterCommit(containerInfo) {},
preparePortalMount: function (containerInfo: unknown): void {
preparePortalMount(containerInfo: unknown) {
throw new Error('Function not implemented.');
},
scheduleTimeout(
Expand Down Expand Up @@ -312,13 +319,24 @@ export function render(
// TODO: remove
(globalThis as any).root = root;

// @ts-expect-error I don't know that's supposed to be passed here…
const container = reconciler.createContainer(root, false, false);
const container = reconciler.createContainer(
root,
0 /* Root Tag */,
null /* hydrationCallbacks */,
false /* isStrictMode */,
null /* concurrentUpdatesByDefaultOverride */,
'' /* identifierPrefix */,
function onRecoverableError(err) {
console.log(err);
},
null /* transitionCallbacks */,
);

reconciler.updateContainer(
createElement(ParentContext.Provider, { value: root }, app),
container,
null,
null,
null /* parentComponent */,
null /* callback */,
);

return root;
Expand Down
Loading

0 comments on commit 5ed03ad

Please sign in to comment.