-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Script with script properties (#6528)
- Loading branch information
1 parent
2a59ded
commit 99469d6
Showing
6 changed files
with
66 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@modern-js/runtime': patch | ||
'app-docmuent': patch | ||
--- | ||
|
||
feat: support script properties with Script | ||
|
||
feat: 支持 script 原生属性 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
import React from 'react'; | ||
import { renderToString } from 'react-dom/server'; | ||
import { | ||
DOCUMENT_SCRIPT_ATTRIBUTES_END, | ||
DOCUMENT_SCRIPT_ATTRIBUTES_START, | ||
DOCUMENT_SCRIPT_PLACEHOLDER_END, | ||
DOCUMENT_SCRIPT_PLACEHOLDER_START, | ||
} from './constants'; | ||
|
||
export function Script(props: { content: () => void }) { | ||
const { content } = props; | ||
const contentStr = content.toString(); | ||
const contentIIFE = encodeURIComponent(`(${contentStr})()`); | ||
export function Script(props: DocumentScriptProps) { | ||
const { content, ...rests } = props; | ||
const contentStr = content?.toString(); | ||
const contentIIFE = contentStr?.length | ||
? encodeURIComponent(`(${contentStr})()`) | ||
: ''; | ||
const scriptProperties = renderToString(<script {...rests} />); | ||
const scriptpPropertiesStr = encodeURIComponent( | ||
scriptProperties.replace('<script ', '').replace('></script>', ''), | ||
); | ||
|
||
return ( | ||
<> | ||
{`${DOCUMENT_SCRIPT_PLACEHOLDER_START}`} | ||
{`${DOCUMENT_SCRIPT_ATTRIBUTES_START}${scriptpPropertiesStr}${DOCUMENT_SCRIPT_ATTRIBUTES_END}`} | ||
{`${contentIIFE}`} | ||
{`${DOCUMENT_SCRIPT_PLACEHOLDER_END}`} | ||
</> | ||
); | ||
} | ||
|
||
// tofix: 之前错误的给了 content 作为 Script 组件的 IIFE 载体,但 content 在 htmlelement.meta 上有含义 | ||
export interface DocumentScriptProps | ||
extends Omit<React.ScriptHTMLAttributes<HTMLScriptElement>, 'content'> { | ||
content?: () => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters