Skip to content

Commit 30bc714

Browse files
author
guqiankun.gqk
committed
chore: update demo
1 parent 4f1f733 commit 30bc714

6 files changed

Lines changed: 263 additions & 215 deletions

File tree

packages/alex/src/api/renderApp.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,38 @@ export const AppRenderer: React.FC<IAppRendererProps> = ({ onLoad, Landing, ...o
128128
);
129129
};
130130

131+
const appContainer = document.createElement('div')
132+
appContainer.style.width = '100%'
133+
appContainer.style.height = '100%'
134+
135+
let appMounted = false
136+
137+
export const KeepLive: React.FC = (props) => {
138+
const anchorRef = useRef<HTMLDivElement>(null)
139+
useEffect(() => {
140+
if (!appMounted) {
141+
appMounted = true
142+
ReactDOM.render(<>{props.children}</>, appContainer)
143+
}
144+
145+
anchorRef?.current?.insertAdjacentElement('afterend', appContainer)
146+
147+
return () => {
148+
try {
149+
if (anchorRef?.current?.parentNode !== null) {
150+
anchorRef?.current?.parentNode.removeChild(appContainer);
151+
}
152+
} catch (error) {
153+
console.error(error)
154+
}
155+
}
156+
}, [])
157+
158+
return <div ref={anchorRef} />
159+
}
160+
161+
162+
131163
// 缓存apprender 每次渲染都不卸载组件
132164
export const AppRenderer2: React.FC<IAppRendererProps> = ({ onLoad, Landing, ...opts }) => {
133165
const app = useConstant(() =>

packages/integrations/src/sql/index.tsx

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useMemo, useRef, useState } from 'react';
22
import ReactDOM from 'react-dom';
3-
import { IAppInstance, AppRenderer, SlotLocation, AppRenderer2 } from '@alipay/alex';
3+
import { IAppInstance, AppRenderer, SlotLocation, AppRenderer2, KeepLive } from '@alipay/alex';
44
import '@alipay/alex/languages/sql';
55
import {
66
CompletionItemKind,
@@ -9,12 +9,13 @@ import {
99
setMonacoEnvironment,
1010
} from '@alipay/alex-sql-service';
1111
import { Button } from '@opensumi/ide-components';
12-
import SqlPlugin from './sql.plugin';
12+
import SqlPlugin from './sql.plugin';
1313
import { IEditor } from '@opensumi/ide-editor';
14-
import { Popover, Radio } from 'antd';
14+
import { Popover, Radio, Menu } from 'antd';
1515
import 'antd/dist/antd.css';
1616
import odcTheme from '@alipay/alex/extensions/alex.odc-theme';
1717
import { SQLRender } from './sqlRender';
18+
import { Tabs } from 'antd';
1819

1920
setMonacoEnvironment();
2021

@@ -24,15 +25,13 @@ const layoutConfig = {
2425
},
2526
};
2627

27-
28-
2928
const App = () => {
30-
const PluginID = 1
29+
const PluginID = '';
3130

3231
const SQLPlugin = useMemo(() => {
3332
return new SqlPlugin(PluginID);
3433
}, []);
35-
34+
3635
const [fontValue, setFontValue] = useState(16);
3736
const [encoding, setEncoding] = useState('utf8');
3837
const [editorNumber, setEditorNumber] = useState(1);
@@ -62,9 +61,6 @@ const App = () => {
6261
]);
6362
}
6463

65-
66-
67-
6864
const onChangeFont = (e) => {
6965
setFontValue(e.target.value);
7066
updatePrefeence('editor.fontSize', e.target.value);
@@ -79,29 +75,28 @@ const App = () => {
7975
setEditorNumber(editorNumber + 1);
8076
}
8177

82-
8378
function format() {
8479
SQLPlugin.commands?.executeCommand('editor.action.formatDocument');
8580
}
86-
81+
8782
function updatePrefeence(perferenceName, value) {
8883
// 设置首选项
8984
SQLPlugin.commands?.executeCommand('alex.setDefaultPreference', perferenceName, value);
9085
}
91-
86+
9287
async function addLine() {
9388
const editor = (await SQLPlugin.commands?.executeCommand('alex.sql.editor')) as IEditor;
9489
editor?.monacoEditor.trigger('editor', 'type', { text: '\n' });
9590
}
96-
91+
9792
function openFile() {
9893
/** COMMAND alex.sql.open
9994
* @param {string} uri - 文件uri
10095
* @param {string} content - 文件内容 无内容时创建并注入默认内容
10196
*/
10297
SQLPlugin.commands?.executeCommand('alex.sql.open', 'test1.sql', '默认内容');
10398
}
104-
99+
105100
async function editor() {
106101
const editor = (await SQLPlugin.commands?.executeCommand('alex.sql.editor')) as IEditor;
107102
console.log(editor?.monacoEditor.getValue());
@@ -122,12 +117,28 @@ const App = () => {
122117
</Radio.Group>
123118
</div>
124119
);
120+
const items = [
121+
{
122+
label: 'Navigation One',
123+
key: '1',
124+
},
125+
{
126+
label: 'Navigation Two',
127+
key: '2',
128+
},
129+
];
130+
const [current, setCurrent] = useState('1');
131+
132+
const menuCick = (e) => {
133+
console.log('click ', e);
134+
setCurrent(e.key);
135+
};
125136

126137
return (
127138
<div style={{ height: '100%', overflow: 'scroll' }}>
128139
<div style={{ height: '300px' }}>
129140
<div style={{ margin: '20px' }}>
130-
<Button onClick={() => format()}>格式化</Button>
141+
{/* <Button onClick={() => format()}>格式化</Button>
131142
<Button onClick={() => addLine()}>添加行</Button>
132143
<Button onClick={() => openFile()}>打开文件</Button>
133144
<Button onClick={() => editor()}>获取当前内容</Button>
@@ -137,9 +148,17 @@ const App = () => {
137148
</Popover>
138149
<Button onClick={() => changeTables()}>change suggest Tables</Button>
139150
<Button onClick={() => window.reset()}>reset </Button>
140-
<Button onClick={() => editorNumberUpdate()}>添加编辑器</Button>
151+
<Button onClick={() => editorNumberUpdate()}>添加编辑器</Button> */}
152+
<Menu onClick={menuCick} selectedKeys={[current]} mode="horizontal" items={items} />
153+
<div style={{ display: `${current === '1' ? 'block' : 'none'}` }}>
154+
<SQLRender id="1" />
155+
</div>
156+
<div style={{ display: `${current === '2' ? 'block' : 'none'}` }}>
157+
<SQLRender id="2" />
158+
</div>
141159
</div>
142-
<AppRenderer2
160+
161+
{/* <AppRenderer2
143162
onLoad={(app) => {
144163
window.app = app;
145164
}}
@@ -316,23 +335,8 @@ const App = () => {
316335
},
317336
},
318337
}}
319-
/>
338+
/> */}
320339
</div>
321-
{editorNumber > 1 ? (
322-
<div>
323-
<SQLRender />
324-
</div>
325-
) : null}
326-
{editorNumber > 2 ? (
327-
<div>
328-
<SQLRender />
329-
</div>
330-
) : null}
331-
{editorNumber > 3 ? (
332-
<div>
333-
<SQLRender />
334-
</div>
335-
) : null}
336340
</div>
337341
);
338342
};

packages/integrations/src/sql/sql.plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export default class SQLPlugin implements IPluginModule {
77
* 插件 ID,用于唯一标识插件
88
*/
99
PLUGIN_ID = 'sql-plugin';
10-
private _id: number;
10+
private _id: string;
1111
commands: IPluginAPI['commands'];
1212
static api: any;
1313

1414
constructor(
15-
id: number
15+
id: string
1616
) {
1717
this._id = id;
1818
}

0 commit comments

Comments
 (0)