-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
833 additions
and
92 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
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,114 @@ | ||
/** | ||
* defaultShowCode: true | ||
*/ | ||
import DSlate, { DefaultToolbar, DSlateRef } from '@dslate/antd'; | ||
import { Button, ConfigProvider, Input, Space, theme } from 'antd'; | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import type { Descendant } from 'slate'; | ||
|
||
export default () => { | ||
const [resultWeapp, setResultWeapp] = useState<string>(); | ||
const [resultHtml, setResultHtml] = useState<string>(); | ||
const [value, setValue] = useState<Descendant[]>([ | ||
{ | ||
type: 'paragraph', | ||
children: [ | ||
{ text: '当' }, | ||
{ type: 'latex', children: [{ text: '' }], formula: 'b=1' }, | ||
{ text: '、' }, | ||
{ type: 'latex', children: [{ text: '' }], formula: 'c=2' }, | ||
{ text: '时,请计算' }, | ||
{ type: 'latex', children: [{ text: '' }], formula: 'a=b+c' }, | ||
{ text: '的结果?' }, | ||
], | ||
}, | ||
]); | ||
|
||
const ref = useRef<DSlateRef>(null); | ||
const [mode, setMode] = useState( | ||
document.documentElement.getAttribute('data-prefers-color') ?? 'light', | ||
); | ||
|
||
const switchMode = () => { | ||
const doc = document.documentElement; | ||
const attrName = 'data-prefers-color'; | ||
if (!doc.hasAttribute(attrName) || doc.getAttribute(attrName) === 'dark') { | ||
doc.setAttribute(attrName, 'light'); | ||
setMode('light'); | ||
} else { | ||
doc.setAttribute(attrName, 'dark'); | ||
setMode('dark'); | ||
} | ||
}; | ||
|
||
const watch = (mutationsList: any[]) => { | ||
const attrName = 'data-prefers-color'; | ||
for (const mutation of mutationsList) { | ||
if (mutation.attributeName === attrName) { | ||
setMode(document.documentElement.getAttribute(attrName) ?? 'light'); | ||
} | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
let observer: any; | ||
if (MutationObserver) { | ||
observer = new MutationObserver(watch); | ||
// 以上述配置开始观察目标节点 | ||
observer.observe(document.documentElement, { attributes: true }); | ||
// 之后,可停止观察 | ||
} | ||
return () => observer?.disconnect?.(); | ||
}, []); | ||
|
||
return ( | ||
<ConfigProvider | ||
theme={{ | ||
algorithm: | ||
mode === 'dark' ? theme.darkAlgorithm : theme.defaultAlgorithm, | ||
}} | ||
> | ||
<div> | ||
<DSlate | ||
toolbar={[...DefaultToolbar, 'latex']} | ||
ref={ref} | ||
value={value} | ||
onChange={setValue} | ||
/> | ||
<br /> | ||
<Space> | ||
<Button | ||
onClick={() => { | ||
setResultWeapp( | ||
JSON.stringify( | ||
ref.current?.serializeWeapp({ | ||
children: value, | ||
}), | ||
null, | ||
2, | ||
), | ||
); | ||
setResultHtml( | ||
ref.current?.serialize?.({ | ||
children: value, | ||
}), | ||
); | ||
}} | ||
> | ||
转格式 | ||
</Button> | ||
<Button onClick={switchMode}> | ||
{mode === 'dark' ? '亮色模式' : '暗色模式'} | ||
</Button> | ||
</Space> | ||
<br /> | ||
<p>小程序结果:</p> | ||
<Input.TextArea value={resultWeapp} autoSize readOnly /> | ||
|
||
<br /> | ||
<p>html结果:</p> | ||
<Input.TextArea value={resultHtml} autoSize readOnly /> | ||
</div> | ||
</ConfigProvider> | ||
); | ||
}; |
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,17 @@ | ||
--- | ||
title: 题目编排 | ||
order: 7 | ||
nav: 文档 | ||
--- | ||
|
||
# 题目编排 | ||
|
||
基于 DSlate 的 Antd 风格包,支持 html 渲染与小程序 RichNode 渲染模式 | ||
|
||
> 小程序请手动引入 katex-mini 包的 css ,具体请看链接:https://github.com/rojer95/katex-mini | ||
> Antd 版本要求:antd >= 5.0 | ||
## Demo | ||
|
||
<code src="../demos/math.tsx"></code> |
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,6 +1,7 @@ | ||
export * from "./editor"; | ||
export * from "./button"; | ||
export * from "./select"; | ||
export * from "./divider"; | ||
export * from "./tooltip"; | ||
export * from "./popover"; | ||
export * from './button'; | ||
export * from './divider'; | ||
export * from './editor'; | ||
export * from './input'; | ||
export * from './popover'; | ||
export * from './select'; | ||
export * from './tooltip'; |
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,19 @@ | ||
import { Input as AntdInput } from 'antd'; | ||
import { PropsWithChildren } from 'react'; | ||
|
||
export const Input = ({ | ||
children, | ||
onChange, | ||
...props | ||
}: PropsWithChildren<any>) => { | ||
return ( | ||
<AntdInput | ||
{...props} | ||
onChange={(e) => { | ||
onChange?.(e.target.value); | ||
}} | ||
> | ||
{children} | ||
</AntdInput> | ||
); | ||
}; |
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,19 @@ | ||
import { Input as AntdInput } from 'antd'; | ||
import { PropsWithChildren } from 'react'; | ||
|
||
export const Textarea = ({ | ||
children, | ||
onChange, | ||
...props | ||
}: PropsWithChildren<any>) => { | ||
return ( | ||
<AntdInput.TextArea | ||
{...props} | ||
onChange={(e) => { | ||
onChange?.(e.target.value); | ||
}} | ||
> | ||
{children} | ||
</AntdInput.TextArea> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
export * from "./counter"; | ||
export * from "./divider"; | ||
export * from "./input-number"; | ||
export * from "./input"; | ||
export * from "./popover"; | ||
export * from "./progress"; | ||
export * from "./tooltip"; | ||
export * from "./icon"; | ||
export * from "./toolbar"; | ||
export * from "./editable"; | ||
export * from './counter'; | ||
export * from './divider'; | ||
export * from './editable'; | ||
export * from './icon'; | ||
export * from './input'; | ||
export * from './input-number'; | ||
export * from './popover'; | ||
export * from './progress'; | ||
export * from './textarea'; | ||
export * from './toolbar'; | ||
export * from './tooltip'; |
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,28 @@ | ||
import React, { PropsWithChildren } from 'react'; | ||
import { getElement } from '../element'; | ||
|
||
type InputProps = { | ||
value?: string; | ||
onChange: (value: string) => void; | ||
autosize?: boolean; | ||
[index: string]: any; | ||
}; | ||
|
||
const Textarea = ({ | ||
children, | ||
autosize, | ||
...props | ||
}: PropsWithChildren<InputProps>) => { | ||
const InputElement = getElement('textarea'); | ||
if (!InputElement) return null; | ||
return React.createElement( | ||
InputElement, | ||
{ | ||
...(props || {}), | ||
autoSize: autosize, | ||
} as any, | ||
children, | ||
); | ||
}; | ||
|
||
export { Textarea }; |
Oops, something went wrong.