Skip to content

Commit fa07092

Browse files
committed
feat: Add enableScroll props. #159
1 parent 86829ed commit fa07092

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ ReactDOM.render(<App />, document.getElementById("container"));
359359
- `minHeights?: number=100`: Minimum drag height. The `visiableDragbar=true` value is valid.
360360
- `tabSize?: number=2`: The number of characters to insert when pressing tab key. Default `2` spaces.
361361
- `hideToolbar?: boolean=false`: Option to hide the tool bar.
362+
- `enableScroll?: boolean=true`: Whether to enable scrolling.
362363

363364
### Development
364365

src/Editor.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
7373
* Hide the tool bar
7474
*/
7575
hideToolbar?: boolean;
76+
/** Whether to enable scrolling */
77+
enableScroll?: boolean;
7678
}
7779

7880
function setGroupPopFalse(data: Record<string, boolean> = {}) {
@@ -92,6 +94,7 @@ const InternalMDEditor = (
9294
value: propsValue,
9395
commands = getCommands(),
9496
height = 200,
97+
enableScroll = true,
9598
visiableDragbar = true,
9699
highlightEnable = true,
97100
preview: previewType = 'live',
@@ -121,6 +124,9 @@ const InternalMDEditor = (
121124
});
122125
const container = useRef<HTMLDivElement>(null);
123126
const previewRef = useRef<MarkdownPreviewRef>(null);
127+
const enableScrollRef = useRef(enableScroll);
128+
129+
useMemo(() => (enableScrollRef.current = enableScroll), [enableScroll]);
124130
useEffect(() => {
125131
const stateInit: ContextStore = {};
126132
if (container.current) {
@@ -153,13 +159,7 @@ const InternalMDEditor = (
153159
useMemo(() => fullscreen !== state.fullscreen && dispatch({ fullscreen: fullscreen }), [fullscreen]);
154160

155161
const textareaDomRef = useRef<HTMLDivElement>();
156-
const previewDomRef = useRef<HTMLDivElement>();
157162
const active = useRef<'text' | 'preview'>();
158-
useMemo(() => {
159-
if (previewRef.current) {
160-
previewDomRef.current = previewRef.current.mdp.current || undefined;
161-
}
162-
}, []);
163163

164164
useMemo(() => {
165165
textareaDomRef.current = state.textareaWarp;
@@ -173,9 +173,10 @@ const InternalMDEditor = (
173173
}
174174
}, [state.textareaWarp]);
175175

176-
function handleScroll(e: React.UIEvent<HTMLDivElement>) {
176+
const handleScroll = (scroll: boolean) => (e: React.UIEvent<HTMLDivElement>) => {
177+
if (!enableScrollRef.current) return;
177178
const textareaDom = textareaDomRef.current;
178-
const previewDom = previewDomRef.current;
179+
const previewDom = previewRef.current ? previewRef.current.mdp.current : undefined;
179180
if (textareaDom && previewDom) {
180181
const scale =
181182
(textareaDom.scrollHeight - textareaDom.offsetHeight) / (previewDom.scrollHeight - previewDom.offsetHeight);
@@ -193,7 +194,8 @@ const InternalMDEditor = (
193194
}
194195
dispatch({ scrollTop });
195196
}
196-
}
197+
};
198+
197199
return (
198200
<EditorContext.Provider value={{ ...state, dispatch }}>
199201
<div
@@ -216,13 +218,14 @@ const InternalMDEditor = (
216218
prefixCls={prefixCls}
217219
autoFocus={autoFocus}
218220
{...textareaProps}
219-
onScroll={handleScroll}
221+
onScroll={enableScroll ? handleScroll(enableScroll) : undefined}
220222
/>
221223
)}
222224
{/(live|preview)/.test(state.preview || '') && (
223225
<MarkdownPreview
224226
{...(previewOptions as unknown)}
225-
onScroll={handleScroll}
227+
// onScroll={handleScroll}
228+
onScroll={enableScroll ? handleScroll(enableScroll) : undefined}
226229
ref={previewRef}
227230
source={state.markdown || ''}
228231
className={`${prefixCls}-preview`}

website/Exmaple.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Exmaple = (props = {} as { mdStr: string }) => {
88
visiableDragbar: true,
99
hideToolbar: true,
1010
highlightEnable: true,
11+
enableScroll: true,
1112
value: props.mdStr || '',
1213
preview: 'live',
1314
});
@@ -25,6 +26,7 @@ const Exmaple = (props = {} as { mdStr: string }) => {
2526
height={400}
2627
highlightEnable={state.highlightEnable}
2728
hideToolbar={!state.hideToolbar}
29+
enableScroll={state.enableScroll}
2830
visiableDragbar={state.visiableDragbar}
2931
textareaProps={{
3032
placeholder: 'Please enter Markdown text',
@@ -55,6 +57,16 @@ const Exmaple = (props = {} as { mdStr: string }) => {
5557
/>
5658
{state.highlightEnable ? 'Enable' : 'Unenable'} highlight
5759
</label>
60+
<label>
61+
<input
62+
type="checkbox"
63+
checked={state.enableScroll}
64+
onChange={(e) => {
65+
setVisiable({ ...state, enableScroll: e.target.checked });
66+
}}
67+
/>
68+
{state.enableScroll ? 'Enable' : 'Unenable'} Scroll
69+
</label>
5870
<label>
5971
<input
6072
type="checkbox"

0 commit comments

Comments
 (0)