Skip to content

Commit

Permalink
✨ 内置npm,替代本地npm
Browse files Browse the repository at this point in the history
♻️ 优化分离插件窗口交互
  • Loading branch information
muwoo committed Oct 26, 2023
1 parent 289165d commit a3a7dfd
Show file tree
Hide file tree
Showing 12 changed files with 2,289 additions and 346 deletions.
2 changes: 1 addition & 1 deletion detach/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
<title></title>
</head>
<body>
<noscript>
Expand Down
41 changes: 41 additions & 0 deletions detach/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,47 @@ Object.assign(window, {
plugInfo.value.subInput = null;
},
});
window.enterFullScreenTrigger = () => {
document.querySelector('.detach').classList.remove('darwin');
};
window.leaveFullScreenTrigger = () => {
const titleDom = document.querySelector('.detach');
if (!titleDom.classList.contains('darwin')) {
titleDom.classList.add('darwin');
}
};
window.maximizeTrigger = () => {
const btnMaximize = document.querySelector('.maximize')
if (!btnMaximize || btnMaximize.classList.contains('unmaximize')) return;
btnMaximize.classList.add('unmaximize');
};
window.unmaximizeTrigger = () => {
const btnMaximize = document.querySelector('.maximize');
if (!btnMaximize) return;
btnMaximize.classList.remove('unmaximize');
};
if (process.platform === 'darwin') {
window.onkeydown = (e) => {
if (e.code === 'Escape') {
ipcRenderer.send('detach:service', { type: 'endFullScreen' });
return;
}
if (e.metaKey && (e.code === 'KeyW' || e.code === 'KeyQ')) {
window.handle.close()
}
}
} else {
window.onkeydown = (e) => {
if (e.ctrlKey && e.code === 'KeyW') {
window.handle.close()
return
}
}
}
</script>

<style>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubick",
"version": "4.0.10",
"version": "4.1.0-beta.1",
"author": "muwoo <2424880409@qq.com>",
"private": true,
"scripts": {
Expand Down Expand Up @@ -33,6 +33,7 @@
"lodash.throttle": "^4.1.1",
"memorystream": "^0.3.1",
"node-key-sender": "^1.0.11",
"npm": "6.14.7",
"pinyin-match": "^1.2.4",
"pouchdb": "^7.2.2",
"pouchdb-load": "^1.4.6",
Expand Down
2 changes: 1 addition & 1 deletion public/detach/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="favicon.ico"><title>detach</title><link href="css/app.65d55ce4.css" rel="preload" as="style"><link href="js/app.bc494a66.js" rel="preload" as="script"><link href="js/chunk-vendors.b36194a6.js" rel="preload" as="script"><link href="css/app.65d55ce4.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but detach doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="js/chunk-vendors.b36194a6.js"></script><script src="js/app.bc494a66.js"></script></body></html>
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="favicon.ico"><title></title><link href="css/app.65d55ce4.css" rel="preload" as="style"><link href="js/app.f4fdc34a.js" rel="preload" as="script"><link href="js/chunk-vendors.b36194a6.js" rel="preload" as="script"><link href="css/app.65d55ce4.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but detach doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="js/chunk-vendors.b36194a6.js"></script><script src="js/app.f4fdc34a.js"></script></body></html>
1 change: 0 additions & 1 deletion public/detach/js/app.bc494a66.js

This file was deleted.

1 change: 0 additions & 1 deletion src/core/app-search/darwin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import getMacApps from './get-mac-app';
import fs from 'fs';
import path from 'path';

import translate from './translate';
import os from 'os';

const icondir = path.join(os.tmpdir(), 'ProcessIcon');
Expand Down
92 changes: 58 additions & 34 deletions src/core/plugin-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import path from 'path';
import got from 'got';
import fixPath from 'fix-path';

import spawn from 'cross-spawn';
import { ipcRenderer } from 'electron';
import axios from 'axios';

import npm from 'npm';

fixPath();

/**
Expand Down Expand Up @@ -41,7 +42,7 @@ class AdapterHandler {
}
this.baseDir = options.baseDir;

let register = options.registry || 'https://registry.npm.taobao.org';
let register = options.registry || 'https://registry.npmmirror.com/';

try {
const dbdata = ipcRenderer.sendSync('msg-trigger', {
Expand Down Expand Up @@ -157,44 +158,67 @@ class AdapterHandler {
*/
private async execCommand(cmd: string, modules: string[]): Promise<string> {
return new Promise((resolve: any, reject: any) => {
let args: string[] = [cmd].concat(
const module =
cmd !== 'uninstall' && cmd !== 'link'
? modules.map((m) => `${m}@latest`)
: modules
);
: modules;
const config: any = {
prefix: this.baseDir,
save: true,
cache: path.join(this.baseDir, 'cache'),
};
if (cmd !== 'link') {
args = args
.concat('--color=always')
.concat('--save')
.concat(`--registry=${this.registry}`);
config.registry = this.registry;
}

const npm = spawn('npm', args, {
cwd: this.baseDir,
npm.load(config, function (err) {
npm.commands[cmd](module, function (er, data) {
if (!err) {
console.log(data);
resolve({ code: -1, data });
} else {
reject({ code: -1, data: err });
}
});

npm.on('log', function (message) {
// log installation progress
console.log(message);
});
});

console.log(args);

let output = '';
npm.stdout
.on('data', (data: string) => {
output += data; // 获取输出日志
})
.pipe(process.stdout);

npm.stderr
.on('data', (data: string) => {
output += data; // 获取报错日志
})
.pipe(process.stderr);

npm.on('close', (code: number) => {
if (!code) {
resolve({ code: 0, data: output }); // 如果没有报错就输出正常日志
} else {
reject({ code: code, data: output }); // 如果报错就输出报错日志
}
});
// if (cmd !== 'link') {
// args = args
// .concat('--color=always')
// .concat('--save')
// .concat(`--registry=${this.registry}`);
// }

// const npm = spawn('npm', args, {
// cwd: this.baseDir,
// });
//
// console.log(args);
//
// let output = '';
// npm.stdout
// .on('data', (data: string) => {
// output += data; // 获取输出日志
// })
// .pipe(process.stdout);
//
// npm.stderr
// .on('data', (data: string) => {
// output += data; // 获取报错日志
// })
// .pipe(process.stderr);
//
// npm.on('close', (code: number) => {
// if (!code) {
// resolve({ code: 0, data: output }); // 如果没有报错就输出正常日志
// } else {
// reject({ code: code, data: output }); // 如果报错就输出报错日志
// }
// });
});
}
}
Expand Down
76 changes: 75 additions & 1 deletion src/main/browsers/detach.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BrowserWindow, ipcMain, nativeTheme } from 'electron';
import { BrowserWindow, ipcMain, nativeTheme, screen } from 'electron';
import localConfig from '../common/initLocalConfig';
import commonConst from '@/common/utils/commonConst';
import path from 'path';
import { WINDOW_MIN_HEIGHT } from '@/common/constans/common';
import mainInstance from '@/main';
export default () => {
let win: any;

Expand Down Expand Up @@ -38,6 +40,7 @@ export default () => {
webviewTag: true,
devTools: true,
nodeIntegration: true,
navigateOnDragDrop: true,
spellcheck: false,
},
});
Expand All @@ -55,6 +58,7 @@ export default () => {
});
createWin.on('focus', () => {
win = createWin;
view && win.webContents?.focus();
});

createWin.once('ready-to-show', async () => {
Expand All @@ -71,6 +75,73 @@ export default () => {
);
createWin.show();
});

// 最大化设置
createWin.on('maximize', () => {
createWin.webContents.executeJavaScript('window.maximizeTrigger()');
const view = createWin.getBrowserView();
if (!view) return;
const display = screen.getDisplayMatching(createWin.getBounds());
view.setBounds({
x: 0,
y: WINDOW_MIN_HEIGHT,
width: display.workArea.width,
height: display.workArea.height - WINDOW_MIN_HEIGHT,
});
});
// 最小化
createWin.on('unmaximize', () => {
createWin.webContents.executeJavaScript('window.unmaximizeTrigger()');
const view = createWin.getBrowserView();
if (!view) return;
const bounds = createWin.getBounds();
const display = screen.getDisplayMatching(bounds);
const width =
(display.scaleFactor * bounds.width) % 1 == 0
? bounds.width
: bounds.width - 2;
const height =
(display.scaleFactor * bounds.height) % 1 == 0
? bounds.height
: bounds.height - 2;
view.setBounds({
x: 0,
y: WINDOW_MIN_HEIGHT,
width,
height: height - WINDOW_MIN_HEIGHT,
});
});

createWin.on('page-title-updated', (e) => {
e.preventDefault();
});
createWin.webContents.once('render-process-gone', () => {
createWin.close();
});

if (commonConst.macOS()) {
createWin.on('enter-full-screen', () => {
createWin.webContents.executeJavaScript(
'window.enterFullScreenTrigger()'
);
});
createWin.on('leave-full-screen', () => {
createWin.webContents.executeJavaScript(
'window.leaveFullScreenTrigger()'
);
});
}

view.webContents.on('before-input-event', (event, input) => {
if (input.type !== 'keyDown') return;
if (!(input.meta || input.control || input.shift || input.alt)) {
if (input.key === 'Escape') {
operation.endFullScreen();
}
return;
}
});

const executeHooks = (hook, data) => {
if (!view) return;
const evalJs = `console.log(window.rubick);if(window.rubick && window.rubick.hooks && typeof window.rubick.hooks.on${hook} === 'function' ) {
Expand All @@ -97,6 +168,9 @@ export default () => {
close: () => {
win.close();
},
endFullScreen: () => {
win.isFullScreen() && win.setFullScreen(false);
},
};

return {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ getPluginInfo({
watch([options, pluginHistory, currentPlugin], () => {
currentSelect.value = 0;
if (currentPlugin.value.name) return;
// if (currentPlugin.value.name) return;
nextTick(() => {
ipcRenderer.sendSync('msg-trigger', {
type: 'setExpendHeight',
data: getWindowHeight(
options.value,
pluginLoading.value ? [] : pluginHistory.value
(pluginLoading.value || currentPlugin.value.name) ? [] : pluginHistory.value
),
});
});
Expand Down
1 change: 0 additions & 1 deletion src/renderer/components/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<template #suffix>
<div class="suffix-tool">
<MoreOutlined
v-show="!pluginLoading"
@click="showSeparate()"
class="icon-more"
/>
Expand Down
1 change: 1 addition & 0 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
externals: [
'pouchdb',
'extract-file-icon',
'npm',
'electron-screenshots',
'@electron/remote',
],
Expand Down
Loading

0 comments on commit a3a7dfd

Please sign in to comment.