Skip to content

Commit 687a39c

Browse files
author
Deep-sea-school
committed
兼容桌面端
1 parent fed15c3 commit 687a39c

6 files changed

Lines changed: 247 additions & 70 deletions

File tree

src/components/github-oauth-modal/github-oauth-modal.jsx

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,17 @@ const GitHubOAuthModal = props => {
156156
});
157157
}
158158

159-
// 检查 URL 参数是否包含 OAuth 回调
160-
const params = new URLSearchParams(window.location.search);
161-
if (params.has('code')) {
162-
handleOAuthCallback();
159+
// 检查是否在桌面环境中,如果是,检查是否有待处理的 OAuth 回调
160+
const isDesktop = typeof EditorPreload !== 'undefined';
161+
if (isDesktop) {
162+
// 在桌面环境中,我们不依赖 URL 参数
163+
// 而是依赖主进程发送的 token
164+
} else {
165+
// 检查 URL 参数是否包含 OAuth 回调
166+
const params = new URLSearchParams(window.location.search);
167+
if (params.has('code')) {
168+
handleOAuthCallback();
169+
}
163170
}
164171
}
165172
}, [isOpen]);
@@ -184,6 +191,42 @@ const GitHubOAuthModal = props => {
184191
setIsAuthenticating(false);
185192
}
186193
};
194+
195+
// 桌面环境中的 OAuth 令牌接收处理
196+
React.useEffect(() => {
197+
let unsubscribe = null;
198+
199+
if (isOpen && typeof EditorPreload !== 'undefined') {
200+
// 在桌面环境中监听来自主进程的 OAuth 令牌
201+
unsubscribe = EditorPreload.receiveOAuthToken(async (token) => {
202+
try {
203+
setIsAuthenticating(true);
204+
setError('');
205+
206+
// 使用接收到的令牌处理用户信息
207+
const result = await githubOAuth.processToken(token);
208+
setUserInfo({
209+
...result.user,
210+
email: result.email
211+
});
212+
213+
onSuccess && onSuccess(result);
214+
} catch (err) {
215+
console.error('OAuth token processing failed:', err);
216+
setError(err.message);
217+
onError && onError(err);
218+
} finally {
219+
setIsAuthenticating(false);
220+
}
221+
});
222+
}
223+
224+
return () => {
225+
if (unsubscribe) {
226+
unsubscribe();
227+
}
228+
};
229+
}, [isOpen]);
187230

188231
const handleAuthenticate = async () => {
189232
try {

src/components/gui/gui.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ const GUIComponent = props => {
829829
onClickGitCommit={handleClickGitCommit}
830830
onGitQuickAction={handleGitQuickAction}
831831
showGitQuickButtons={hasGitRepository() && hasGitToken()}
832+
onClickGitHubOAuth={() => setIsOAuthModalOpen(true)}
832833
/>
833834
)}
834835
<Box className={styles.bodyWrapper}>
@@ -1137,6 +1138,8 @@ GUIComponent.propTypes = {
11371138
fontsModalVisible: PropTypes.bool,
11381139
unknownPlatformModalVisible: PropTypes.bool,
11391140
invalidProjectModalVisible: PropTypes.bool,
1141+
githubOAuthModalVisible: PropTypes.bool,
1142+
onClickGitHubOAuth: PropTypes.func,
11401143
vm: PropTypes.instanceOf(VM).isRequired
11411144
};
11421145
GUIComponent.defaultProps = {

src/components/menu-bar/menu-bar.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,29 @@ class MenuBar extends React.Component {
10791079
</span>
10801080
</div>
10811081
</div>}
1082+
{<div className={styles.menuBarItem}>
1083+
<div
1084+
className={classNames(styles.menuBarItem, styles.hoverable)}
1085+
onClick={this.props.onClickGitHubOAuth}
1086+
>
1087+
<svg
1088+
className={styles.githubIcon}
1089+
width="20"
1090+
height="20"
1091+
viewBox="0 0 24 24"
1092+
fill="currentColor"
1093+
>
1094+
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
1095+
</svg>
1096+
<span className={styles.collapsibleLabel}>
1097+
<FormattedMessage
1098+
defaultMessage="GitHub"
1099+
description="Button to open GitHub OAuth"
1100+
id="gui.menuBar.github"
1101+
/>
1102+
</span>
1103+
</div>
1104+
</div>}
10821105
{/* Git 快捷按钮 */}
10831106
{this.props.vm && this.props.vm.runtime && this.props.vm.runtime.platform &&
10841107
this.props.vm.runtime.platform.git && this.props.vm.runtime.platform.git.repository &&
@@ -1197,6 +1220,7 @@ MenuBar.propTypes = {
11971220
onClickSaveAsCopy: PropTypes.func,
11981221
onClickSettings: PropTypes.func,
11991222
onClickSettingsModal: PropTypes.func,
1223+
onClickGitHubOAuth: PropTypes.func,
12001224
onLogOut: PropTypes.func,
12011225
onOpenRegistration: PropTypes.func,
12021226
onOpenTipLibrary: PropTypes.func,

src/containers/gui.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import {
2222
closeCostumeLibrary,
2323
closeBackdropLibrary,
2424
closeTelemetryModal,
25-
openExtensionLibrary
25+
openExtensionLibrary,
26+
openGitHubOAuthModal,
27+
closeGitHubOAuthModal
2628
} from '../reducers/modals';
2729

2830
import FontLoaderHOC from '../lib/font-loader-hoc.jsx';
@@ -101,6 +103,9 @@ class GUI extends React.Component {
101103
return (
102104
<GUIComponent
103105
loading={fetchingProject || isLoading || loadingStateVisible}
106+
githubOAuthModalVisible={githubOAuthModalVisible}
107+
onOpenGitHubOAuthModal={onOpenGitHubOAuthModal}
108+
onRequestCloseGitHubOAuthModal={onRequestCloseGitHubOAuthModal}
104109
{...componentProps}
105110
>
106111
{children}
@@ -132,6 +137,9 @@ GUI.propTypes = {
132137
projectHost: PropTypes.string,
133138
projectId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
134139
telemetryModalVisible: PropTypes.bool,
140+
githubOAuthModalVisible: PropTypes.bool,
141+
onOpenGitHubOAuthModal: PropTypes.func,
142+
onRequestCloseGitHubOAuthModal: PropTypes.func,
135143
vm: PropTypes.instanceOf(VM).isRequired
136144
};
137145

@@ -177,6 +185,7 @@ const mapStateToProps = state => {
177185
fontsModalVisible: state.scratchGui.modals.fontsModal,
178186
unknownPlatformModalVisible: state.scratchGui.modals.unknownPlatformModal,
179187
invalidProjectModalVisible: state.scratchGui.modals.invalidProjectModal,
188+
githubOAuthModalVisible: state.scratchGui.modals.githubOAuthModal,
180189
vm: state.scratchGui.vm
181190
};
182191
};
@@ -188,7 +197,9 @@ const mapDispatchToProps = dispatch => ({
188197
onActivateSoundsTab: () => dispatch(activateTab(SOUNDS_TAB_INDEX)),
189198
onRequestCloseBackdropLibrary: () => dispatch(closeBackdropLibrary()),
190199
onRequestCloseCostumeLibrary: () => dispatch(closeCostumeLibrary()),
191-
onRequestCloseTelemetryModal: () => dispatch(closeTelemetryModal())
200+
onRequestCloseTelemetryModal: () => dispatch(closeTelemetryModal()),
201+
onRequestOpenGitHubOAuthModal: () => dispatch(openGitHubOAuthModal()),
202+
onRequestCloseGitHubOAuthModal: () => dispatch(closeGitHubOAuthModal())
192203
});
193204

194205
const ConnectedGUI = injectIntl(connect(

0 commit comments

Comments
 (0)