-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): plugin playground/enable edit html with modal (#1236)
Co-authored-by: lby <[email protected]>
- Loading branch information
Showing
3 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
web/src/beta/features/PluginPlayground/Code/HtmlEditModal.tsx
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,60 @@ | ||
import { | ||
Button, | ||
CodeInput, | ||
Modal, | ||
ModalPanel | ||
} from "@reearth/beta/lib/reearth-ui"; | ||
import { styled } from "@reearth/services/theme"; | ||
import { FC, useState } from "react"; | ||
|
||
type Props = { | ||
isOpened: boolean; | ||
sourceCode: string; | ||
onClose: () => void; | ||
onSubmit: (value: string) => void; | ||
}; | ||
|
||
const HtmlEditModal: FC<Props> = ({ | ||
isOpened, | ||
onClose, | ||
sourceCode, | ||
onSubmit | ||
}) => { | ||
const [value, setValue] = useState(sourceCode); | ||
return ( | ||
<Modal size="medium" visible={isOpened}> | ||
<ModalPanel | ||
title="HTML Editor" | ||
onCancel={onClose} | ||
actions={ | ||
<> | ||
<Button title="Cancel" onClick={onClose} /> | ||
<Button | ||
title="Submit" | ||
appearance="primary" | ||
onClick={() => { | ||
onSubmit(value); | ||
onClose(); | ||
}} | ||
/> | ||
</> | ||
} | ||
> | ||
<CodeInputWrapper> | ||
<CodeInput | ||
language="html" | ||
value={value} | ||
onChange={(value) => setValue(value ?? "")} | ||
/> | ||
</CodeInputWrapper> | ||
</ModalPanel> | ||
</Modal> | ||
); | ||
}; | ||
|
||
const CodeInputWrapper = styled("div")(() => ({ | ||
height: "100%", | ||
minHeight: "554px" | ||
})); | ||
|
||
export default HtmlEditModal; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const REEARTH_HTML_INJECTION_PATTERN = | ||
/reearth\.(ui|modal|popup)\.show\(\s*(['"`])([^]*?)\2\s*\);/; | ||
|
||
export const extractHtmlFromSourceCode = (sourceCode: string) => { | ||
const match = REEARTH_HTML_INJECTION_PATTERN.exec(sourceCode); | ||
return match?.[3]; | ||
}; | ||
|
||
export const injectHtmlIntoSourceCode = ( | ||
htmlSourceCode: string, | ||
sourceCode: string | ||
) => { | ||
return sourceCode.replace( | ||
REEARTH_HTML_INJECTION_PATTERN, | ||
(_, type) => `reearth.${type}.show(\`${htmlSourceCode}\`);` | ||
); | ||
}; | ||
|
||
export const getLanguageByFileExtension = (fileTitle: string) => { | ||
const ext = fileTitle.split(".").pop(); | ||
switch (ext) { | ||
case "js": | ||
return "javascript"; | ||
case "yml": | ||
case "yaml": | ||
return "yaml"; | ||
default: | ||
return "plaintext"; | ||
} | ||
}; |