Skip to content

Commit

Permalink
feat: change encoding on text file (#110)
Browse files Browse the repository at this point in the history
close AlistGo/alist#4131

---------

Co-authored-by: Andy Hsu <i@nn.ci>
  • Loading branch information
a57495681800 and xhofe authored Aug 27, 2023
1 parent bae9d6e commit 7be0048
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 81 deletions.
73 changes: 73 additions & 0 deletions src/components/EncodingSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Box } from "@hope-ui/solid"
import { SelectWrapper } from "./Base"

export function EncodingSelect(props: {
encoding: string
setEncoding: (encoding: string) => void
}) {
const encodingLabels = [
"utf-8",
"gbk",
"gb18030",
"ibm866",
"iso-8859-2",
"iso-8859-3",
"iso-8859-4",
"iso-8859-5",
"iso-8859-6",
"iso-8859-7",
"iso-8859-8",
"iso-8859-8i",
"iso-8859-10",
"iso-8859-13",
"iso-8859-14",
"iso-8859-15",
"iso-8859-16",
"koi8-r",
"koi8-u",
"macintosh",
"windows-874",
"windows-1250",
"windows-1251",
"windows-1252",
"windows-1253",
"windows-1254",
"windows-1255",
"windows-1256",
"windows-1257",
"windows-1258",
"x-mac-cyrillic",
"big5",
"euc-jp",
"iso-2022-jp",
"shift_jis",
"euc-kr",
"iso-2022-kr",
"utf-16be",
"utf-16le",
"x-user-defined",
"iso-2022-cn",
]
return (
<Box
pos="absolute"
right={0}
top={0}
w="$36"
opacity={0.15}
_hover={{
opacity: 1,
}}
zIndex={1}
>
<SelectWrapper
options={encodingLabels.map((label) => ({
label: label.toLocaleUpperCase(),
value: label,
}))}
value={props.encoding}
onChange={(v) => props.setEncoding(v)}
/>
</Box>
)
}
55 changes: 47 additions & 8 deletions src/components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,59 @@ import reMarkMath from "remark-math"
import rehypeKatex from "rehype-katex"
import "./markdown.css"
import "./katex.css"
import { onMount } from "solid-js"
import {
Show,
createEffect,
createMemo,
createSignal,
on,
onMount,
} from "solid-js"
import { clsx } from "clsx"
import { Box } from "@hope-ui/solid"
import { useParseText } from "~/hooks"
import { EncodingSelect } from "."

export const Markdown = (props: { children?: string; class?: string }) => {
export const Markdown = (props: {
children?: string | ArrayBuffer
class?: string
ext?: string
}) => {
onMount(() => {
hljs.highlightAll()
window.onMDRender && window.onMDRender()
})
const [encoding, setEncoding] = createSignal<string>("utf-8")
const [show, setShow] = createSignal(true)
const { isString, text } = useParseText(props.children)
const convertToMd = (content: string) => {
if (!props.ext || props.ext === "md") {
return "```" + props.ext + "\n" + content + "\n```"
}
return content
}
const md = createMemo(() => convertToMd(text(encoding())))
createEffect(
on(md, () => {
setShow(false)
setTimeout(() => {
setShow(true)
})
}),
)
return (
<SolidMarkdown
class={clsx("markdown-body", props.class)}
remarkPlugins={[remarkGfm, reMarkMath]}
rehypePlugins={[rehypeRaw, rehypeKatex]}
children={props.children}
/>
<Box class="markdown" pos="relative" w="$full">
<Show when={show()}>
<SolidMarkdown
class={clsx("markdown-body", props.class)}
remarkPlugins={[remarkGfm, reMarkMath]}
rehypePlugins={[rehypeRaw, rehypeKatex]}
children={md()}
/>
</Show>
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
</Show>
</Box>
)
}
3 changes: 3 additions & 0 deletions src/components/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const MonacoEditor = (props: MonacoEditorProps) => {
props.onChange?.(monacoEditor.getValue())
})
})
createEffect(() => {
monacoEditor.setValue(props.value)
})

createEffect(() => {
monaco.editor.setTheme(props.theme)
Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from "./ModalTwoInput"
export * from "./Base"
export * from "./Paginator"
export * from "./icons"
export * from "./EncodingSelect"
21 changes: 19 additions & 2 deletions src/hooks/useUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import copy from "copy-to-clipboard"
import { createResource } from "solid-js"
import { getHideFiles, objStore } from "~/store"
import { Obj } from "~/types"
import { fetchText, notify, pathJoin } from "~/utils"
import { decodeText, fetchText, notify, pathJoin } from "~/utils"
import { useT, useLink, useRouter } from "."

export const useUtil = () => {
Expand All @@ -25,10 +25,27 @@ export const useUtil = () => {
}
}

export const useFetchText = () => {
export function useFetchText() {
const { proxyLink } = useLink()
const fetchContent = async () => {
return fetchText(proxyLink(objStore.obj, true))
}
return createResource("", fetchContent)
}

export function useParseText(data?: string | ArrayBuffer) {
const isString = typeof data === "string"
const text = (encoding = "utf-8") => {
if (!data) {
return ""
}
if (isString) {
return data as string
}
return decodeText(data as ArrayBuffer, encoding)
}
return {
isString,
text,
}
}
2 changes: 1 addition & 1 deletion src/pages/home/Readme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Readme = () => {
)
const fetchContent = async (readme: string) => {
let res = {
content: readme,
content: readme as string | ArrayBuffer,
}
if (/^https?:\/\//g.test(readme)) {
res = await fetchText(readme)
Expand Down
35 changes: 24 additions & 11 deletions src/pages/home/previews/html.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import { hope } from "@hope-ui/solid"
import { BoxWithFullScreen, MaybeLoading } from "~/components"
import { useFetchText } from "~/hooks"
import { Show, createSignal } from "solid-js"
import { BoxWithFullScreen, EncodingSelect, MaybeLoading } from "~/components"
import { useFetchText, useParseText } from "~/hooks"

function Html(props: { children?: string | ArrayBuffer }) {
const [encoding, setEncoding] = createSignal<string>("utf-8")
const { isString, text } = useParseText(props.children)
return (
<BoxWithFullScreen w="$full" h="70vh" pos="relative">
<hope.iframe
w="$full"
h="$full"
rounded="$lg"
shadow="$md"
srcdoc={text(encoding())}
/>
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
</Show>
</BoxWithFullScreen>
)
}

const HtmlPreview = () => {
const [content] = useFetchText()

return (
<MaybeLoading loading={content.loading}>
<BoxWithFullScreen w="$full" h="70vh">
<hope.iframe
w="$full"
h="$full"
rounded="$lg"
shadow="$md"
srcdoc={content()?.content}
/>
</BoxWithFullScreen>
<Html>{content()?.content}</Html>
</MaybeLoading>
)
}
Expand Down
8 changes: 1 addition & 7 deletions src/pages/home/previews/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ import { ext } from "~/utils"

const MdPreview = () => {
const [content] = useFetchText()
const convertToMd = (content: string) => {
if (!objStore.obj.name.endsWith(".md")) {
return "```" + ext(objStore.obj.name) + "\n" + content + "\n```"
}
return content
}
return (
<MaybeLoading loading={content.loading}>
<Markdown children={convertToMd(content()?.content ?? "")} />
<Markdown children={content()?.content} ext={ext(objStore.obj.name)} />
</MaybeLoading>
)
}
Expand Down
13 changes: 1 addition & 12 deletions src/pages/home/previews/markdown_with_word_wrap.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { Markdown, MaybeLoading } from "~/components"
import { useFetchText } from "~/hooks"
import { objStore } from "~/store"
import { ext } from "~/utils"

const MdPreview = () => {
const [content] = useFetchText()
const convertToMd = (content: string) => {
if (!objStore.obj.name.endsWith(".md")) {
return "```" + ext(objStore.obj.name) + "\n" + content + "\n```"
}
return content
}
return (
<MaybeLoading loading={content.loading}>
<Markdown
class="word-wrap"
children={convertToMd(content()?.content ?? "")}
/>
<Markdown class="word-wrap" children={content()?.content} />
</MaybeLoading>
)
}
Expand Down
Loading

0 comments on commit 7be0048

Please sign in to comment.