Skip to content

Commit

Permalink
fix: make html semantics layer works in react and angular
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Jul 15, 2024
1 parent 445b59e commit 6324c71
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
:host ::ng-deep .typst-html-semantics {
position: absolute;
z-index: 2;
color: transparent;
font-family: monospace;
white-space: pre;
}

:host ::ng-deep .typst-html-semantics span {
transform-origin: left top;
position: absolute;
display: inline-block;
left: 0;
top: 0;
}

:host ::ng-deep .typst-content-hint {
position: absolute;
display: inline-block;
width: 1px;
height: 1px;
overflow: hidden;
}

:host ::ng-deep .typst-html-semantics a {
position: absolute;
display: inline-block;
}

/* set transparent itself */
:host ::ng-deep .typst-content-group {
pointer-events: visible;
}

:host ::ng-deep .typst-html-semantics span::-moz-selection {
color: transparent;
background: #7db9dea0;
}

:host ::ng-deep .typst-html-semantics span::selection {
color: transparent;
background: #7db9dea0;
}

:host ::ng-deep .typst-html-semantics *::-moz-selection {
color: transparent;
background: transparent;
}

:host ::ng-deep .typst-html-semantics *::selection {
color: transparent;
background: transparent;
}

:host ::ng-deep .typst-content-fallback {
color: transparent;
background: transparent;
}

:host ::ng-deep .pseudo-link,
:host ::ng-deep .typst-text {
pointer-events: none;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let moduleInitOptions: typst.InitOptions = {
@Component({
selector: 'typst-document',
templateUrl: `./typst.document.component.html`,
styles: [],
styleUrls: [`./typst.document.component.css`],
})
export class TypstDocumentComponent {
_artifact: Uint8Array = new Uint8Array(0);
Expand Down
2 changes: 1 addition & 1 deletion packages/typst.react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```typescript
import { TypstDocument } from '@myriaddreamin/typst.react';

export const App = (artifact: string) => {
export const App = (artifact: Uint8Array) => {
return (
<div>
<h1>Demo: Embed Your Typst Document in React</h1>
Expand Down
69 changes: 68 additions & 1 deletion packages/typst.react/src/lib/TypstDocument.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef, useEffect } from 'react';
import { useState, useRef, useEffect } from 'react';
import { withGlobalRenderer } from '@myriaddreamin/typst.ts/dist/esm/contrib/global-renderer.mjs';
import * as typst from '@myriaddreamin/typst.ts';

Expand All @@ -7,6 +7,71 @@ import * as typst from '@myriaddreamin/typst.ts';
// return undefined;
// };

const htmlLayerCss = `
.typst-html-semantics {
position: absolute;
z-index: 2;
color: transparent;
font-family: monospace;
white-space: pre;
}
.typst-html-semantics span {
transform-origin: left top;
position: absolute;
display: inline-block;
left: 0;
top: 0;
}
.typst-content-hint {
position: absolute;
display: inline-block;
width: 1px;
height: 1px;
overflow: hidden;
}
.typst-html-semantics a {
position: absolute;
display: inline-block;
}
/* set transparent itself */
.typst-content-group {
pointer-events: visible;
}
.typst-html-semantics span::-moz-selection {
color: transparent;
background: #7db9dea0;
}
.typst-html-semantics span::selection {
color: transparent;
background: #7db9dea0;
}
.typst-html-semantics *::-moz-selection {
color: transparent;
background: transparent;
}
.typst-html-semantics *::selection {
color: transparent;
background: transparent;
}
.typst-content-fallback {
color: transparent;
background: transparent;
}
.pseudo-link,
.typst-text {
pointer-events: none;
}`;

export interface TypstDocumentProps {
fill?: string;
artifact: Uint8Array;
Expand Down Expand Up @@ -88,6 +153,8 @@ export const TypstDocument = ({ fill, artifact, format }: TypstDocumentProps) =>

return (
<div>
{/* todo: remove this embedded css */}
<style>{htmlLayerCss}</style>
<div className="typst-app" style={{ height: '0' }} ref={displayDivRef}></div>
</div>
);
Expand Down

0 comments on commit 6324c71

Please sign in to comment.