Skip to content

Commit

Permalink
fix: 🐛 解决Antd与Semi同时使用时基础组织注册的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer95 committed Jan 8, 2024
1 parent 6a0b634 commit a99afad
Show file tree
Hide file tree
Showing 28 changed files with 3,322 additions and 3,645 deletions.
2 changes: 1 addition & 1 deletion docs/demos/antd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default () => {
>
<div>
<Form.Item label="普通输入框">
<Input />
<Input placeholder="输入框" />
</Form.Item>

<DSlate ref={ref} value={value} onChange={setValue} />
Expand Down
4 changes: 2 additions & 2 deletions docs/demos/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default () => {
]);

return (
<div>
<>
<DSlateAntd
value={value}
onChange={setValue}
Expand All @@ -25,6 +25,6 @@ export default () => {
onChange={setValue}
placeholder="Semi 风格编辑器"
/>
</div>
</>
);
};
121 changes: 38 additions & 83 deletions docs/demos/math.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* 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 { Button, Input, Space } from 'antd';
import React, { useRef, useState } from 'react';
import type { Descendant } from 'slate';

export default () => {
Expand Down Expand Up @@ -72,92 +72,47 @@ export default () => {
]);

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={() => {
console.log(JSON.stringify(value));
<div>
<DSlate
toolbar={[...DefaultToolbar, 'latex']}
ref={ref}
value={value}
onChange={setValue}
/>
<br />
<Space>
<Button
onClick={() => {
console.log(JSON.stringify(value));

setResultWeapp(
JSON.stringify(
ref.current?.serializeWeapp({
children: value,
}),
null,
2,
),
);
setResultHtml(
ref.current?.serialize?.({
setResultWeapp(
JSON.stringify(
ref.current?.serializeWeapp({
children: value,
}),
);
}}
>
转格式
</Button>
<Button onClick={switchMode}>
{mode === 'dark' ? '亮色模式' : '暗色模式'}
</Button>
</Space>
<br />
<p>小程序结果:</p>
<Input.TextArea value={resultWeapp} autoSize readOnly />
null,
2,
),
);
setResultHtml(
ref.current?.serialize?.({
children: value,
}),
);
}}
>
转格式
</Button>
</Space>
<br />
<p>小程序结果:</p>
<Input.TextArea value={resultWeapp} autoSize readOnly />

<br />
<p>html结果:</p>
<Input.TextArea value={resultHtml} autoSize readOnly />
</div>
</ConfigProvider>
<br />
<p>html结果:</p>
<Input.TextArea value={resultHtml} autoSize readOnly />
</div>
);
};
2 changes: 1 addition & 1 deletion docs/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ $ npm i @dslate/semi --save # Semi 风格

## 在项目中使用

<code src="../demos/base.tsx"></code>
<code src="../demos/base.tsx" background="#FFFFFF"></code>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@types/react": "^18.2.13",
"@types/react-dom": "^18.2.6",
"@umijs/lint": "^4.0.71",
"antd": "^5.1.7",
"antd": "^5.12.0",
"dumi": "^2.2.1",
"eslint": "^8.43.0",
"gh-pages": "^5.0.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/antd/lib/components/button.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { PropsWithChildren } from "react";
import { Button as AntdButton, theme } from "antd";
import { Button as AntdButton, theme } from 'antd';
import { PropsWithChildren } from 'react';

const { useToken } = theme;

export const Button = ({
children,
active,
Expand Down
6 changes: 3 additions & 3 deletions packages/antd/lib/components/divider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Divider as AntdDivider } from "antd";
import React, { PropsWithChildren } from "react";
import { Divider as AntdDivider } from 'antd';
import { PropsWithChildren } from 'react';

export const Divider = ({
layout,
}: PropsWithChildren<{
layout?: "vertical" | "horizontal";
layout?: 'vertical' | 'horizontal';
}>) => {
return <AntdDivider type={layout} />;
};
33 changes: 19 additions & 14 deletions packages/antd/lib/components/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import { forwardRef } from "react";
import { theme } from "antd";
import { useFocused } from "slate-react";
import { DSlateRef, DSlateCore } from "@dslate/core";
import classNames from "classnames";
import styled from "styled-components";
import { Toolbar, Progress, Counter, Editable } from "@dslate/component";
import { Counter, Editable, Progress, Toolbar } from '@dslate/component';
import { DSlateCore, DSlateRef } from '@dslate/core';
import { theme } from 'antd';
import classNames from 'classnames';
import { forwardRef } from 'react';
import { useFocused } from 'slate-react';
import styled from 'styled-components';

import type { AntdStyleDSlateProps } from "../typing";
import type { AntdStyleDSlateProps } from '../typing';

const StyledComponent = styled.div<{ token: any }>`
border-radius: ${(props) => `${props.token.borderRadius}px`};
border-width: ${(props) => `${props.token.lineWidth}px`};
border-style: ${(props) => `${props.token.lineType}`};
border-color: ${(props) => `${props.token.colorBorder}`};
background-color: ${(props) => `${props.token.colorBgContainer}`};
transition: ${(props) => `all ${props.token.motionDurationMid}`};
&.focus {
border-color: ${(props) => props.token.colorPrimaryHover};
border-color: ${(props) => `${props.token.colorPrimary}`};
box-shadow: ${(props) =>
`0 0 0 ${props.token.controlOutlineWidth}px ${props.token.controlOutline}`};
border-inline-end-width: ${(props) => props.token.lineWidth};
outline: 0;
}
&:hover {
border-color: ${(props) => props.token.colorPrimaryHover};
}
&.borderless {
background-color: transparent;
border: none;
Expand Down Expand Up @@ -60,11 +65,11 @@ const StyledComponent = styled.div<{ token: any }>`
((props.token.controlHeight -
props.token.fontSize * props.token.lineHeight) /
2) *
10
10,
) /
10 -
props.token.lineWidth,
3
3,
)}px ${props.token.paddingSM - props.token.lineWidth}px`};
h1 {
Expand Down Expand Up @@ -110,7 +115,7 @@ const AntdStyleEditor = ({
placeholder,
toolbar,
className,
}: Omit<AntdStyleDSlateProps, "value" | "onChange">) => {
}: Omit<AntdStyleDSlateProps, 'value' | 'onChange'>) => {
const focused = useFocused();
const { token } = useToken();

Expand Down Expand Up @@ -153,7 +158,7 @@ const AntdEditor = forwardRef<DSlateRef, AntdStyleDSlateProps>(
<AntdStyleEditor {...rest} disabled={disabled} />
</DSlateCore>
);
}
},
);

export { AntdEditor };
30 changes: 15 additions & 15 deletions packages/antd/lib/components/popover.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Popover as AntdPopover } from "antd";
import React, { PropsWithChildren } from "react";
import { Popover as AntdPopover } from 'antd';
import React, { PropsWithChildren } from 'react';

export const Popover = ({
content,
Expand All @@ -10,20 +10,20 @@ export const Popover = ({
}: PropsWithChildren<{
content?: React.ReactNode;
position?:
| "top"
| "topLeft"
| "topRight"
| "left"
| "leftTop"
| "leftBottom"
| "right"
| "rightTop"
| "rightBottom"
| "bottom"
| "bottomLeft"
| "bottomRight";
| 'top'
| 'topLeft'
| 'topRight'
| 'left'
| 'leftTop'
| 'leftBottom'
| 'right'
| 'rightTop'
| 'rightBottom'
| 'bottom'
| 'bottomLeft'
| 'bottomRight';

trigger?: string;
trigger?: any;
visible?: boolean;
}>) => {
return (
Expand Down
28 changes: 14 additions & 14 deletions packages/antd/lib/components/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Tooltip as AntdTooltip } from "antd";
import React, { PropsWithChildren } from "react";
import { Tooltip as AntdTooltip } from 'antd';
import { PropsWithChildren } from 'react';

export const Tooltip = ({
content,
Expand All @@ -8,18 +8,18 @@ export const Tooltip = ({
}: PropsWithChildren<{
content?: string;
position?:
| "top"
| "topLeft"
| "topRight"
| "left"
| "leftTop"
| "leftBottom"
| "right"
| "rightTop"
| "rightBottom"
| "bottom"
| "bottomLeft"
| "bottomRight";
| 'top'
| 'topLeft'
| 'topRight'
| 'left'
| 'leftTop'
| 'leftBottom'
| 'right'
| 'rightTop'
| 'rightBottom'
| 'bottom'
| 'bottomLeft'
| 'bottomRight';
}>) => {
return (
<AntdTooltip placement={position} title={content} destroyTooltipOnHide>
Expand Down
Loading

0 comments on commit a99afad

Please sign in to comment.