Skip to content

Commit

Permalink
Merge pull request #8 from BUPTlhuanyu/restructure
Browse files Browse the repository at this point in the history
Restructure
  • Loading branch information
BUPTlhuanyu authored Jul 1, 2021
2 parents 879048a + 1511121 commit ca1b0b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions packages/views/src/components/useCodemirror/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useEffect, useRef} from 'react';
import React, {useState, useEffect, useRef, useCallback} from 'react';
import codemirror, {createCodemirror} from './code';

interface CodemirrorObj {
Expand Down Expand Up @@ -40,9 +40,24 @@ export default function useCodemirror(): CodemirrorObj {
const [count, setCount] = useState<number>(0);
const [scroll, setScroll] = useState<any>({
scrollTop: 0,
scrollHeight: 0
scrollHeight: 0,
clientHeight: 0
});
const [editor, setEditor] = useState<codemirror.Editor | null>(null);
const updateScrollInfo = useCallback(
(editor: codemirror.Editor | null) => {
if (editor) {
const {clientHeight, height, top} = editor.getScrollInfo();
console.log('updateScrollInfo', clientHeight, height, top);
setScroll({
scrollTop: top,
clientHeight,
scrollHeight: height
});
}
},
[setScroll, editor],
);
useEffect(() => {
if (!isMounted.current || !ele) {
isMounted.current = true;
Expand All @@ -55,14 +70,10 @@ export default function useCodemirror(): CodemirrorObj {
setCode(code);
setCount(count);
});
editor.on('scroll', (editor: any) => {
const {clientHeight, height, top} = editor.getScrollInfo();
setScroll({
scrollTop: top,
clientHeight,
scrollHeight: height
});
});
editor.on('update', updateScrollInfo);
editor.on('scroll', updateScrollInfo);
setEditor(editor);
updateScrollInfo(editor);
// 自定义事件
// Object.defineProperty(editor, 'codeSettedEmitter', {
// value: () => {
Expand All @@ -71,7 +82,6 @@ export default function useCodemirror(): CodemirrorObj {
// });
// (editor as any).codeSettedEmitter(editor, 'codeSetted'); // 触发
// editor.on('codeSetted', ()=>{}); // 监听
setEditor(editor);
}
}, [ele]);

Expand Down
2 changes: 1 addition & 1 deletion packages/workbench-electron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "workbench-electron",
"version": "0.0.0",
"version": "0.0.5",
"description": "Pandora Workbench",
"productName": "Pandora Workbench",
"private": true,
Expand Down

0 comments on commit ca1b0b6

Please sign in to comment.