Skip to content

Commit

Permalink
fix: text leaf to detect parent correctly (redotvideo#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
hkonsti authored Dec 16, 2024
1 parent 0f587db commit f9b3764
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/2d/src/lib/components/Txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {is} from '../utils';
import type {Node} from './Node';
import type {ShapeProps} from './Shape';
import {Shape} from './Shape';
import {TxtLeaf} from './TxtLeaf';
import {TXT_TYPE, TxtLeaf} from './TxtLeaf';
import type {ComponentChildren} from './types';

type TxtChildren = string | Node | (string | Node)[];
Expand All @@ -24,6 +24,8 @@ export interface TxtProps extends ShapeProps {

@nodeName('Txt')
export class Txt extends Shape {
public readonly [TXT_TYPE] = true;

/**
* Create a bold text node.
*
Expand Down
12 changes: 11 additions & 1 deletion packages/2d/src/lib/components/TxtLeaf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface TxtLeafProps extends ShapeProps {
text?: SignalValue<string>;
}

export const TXT_TYPE = Symbol('Txt');

@nodeName('TxtLeaf')
export class TxtLeaf extends Shape {
@lazy(() => {
Expand Down Expand Up @@ -43,7 +45,15 @@ export class TxtLeaf extends Shape {
@computed()
protected parentTxt() {
const parent = this.parent();
return parent?.constructor.name === 'Txt' ? parent : null;
if (!parent) {
return null;
}

if (!(TXT_TYPE in parent)) {
return null;
}

return parent;
}

protected override async draw(context: CanvasRenderingContext2D) {
Expand Down
3 changes: 2 additions & 1 deletion packages/2d/src/lib/jsx-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function isClassComponent(
return !!fn.prototype?.isClass;
}

export const Fragment = Symbol.for('@revideo/2d/fragment');
export const Fragment: FunctionComponent = ({children}) => children;

export function jsx(
type: NodeConstructor | FunctionComponent | typeof Fragment,
config: JSXProps,
Expand Down

0 comments on commit f9b3764

Please sign in to comment.