-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrap Casbin editor into a desktop app via Electron (#109)
* feat(main): add Electron integration and preload script * refactor: Refactor file paths and error handling in main.js * chore: Update next.config.mjs with comments and code formatting * chore: Update README.md with Electron build instructions * chore:remove package-lock.json * chore: Update license and build configuration * Update README.md --------- Co-authored-by: Yang Luo <[email protected]>
- Loading branch information
1 parent
6cd04f5
commit a8dd692
Showing
8 changed files
with
1,811 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
# production | ||
/build | ||
/dist | ||
|
||
# misc | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2024 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
const { app, BrowserWindow, protocol } = require('electron'); | ||
const path = require('path'); | ||
|
||
protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }]); | ||
|
||
let appServe; | ||
let isDev; | ||
|
||
const loadModules = async () => { | ||
isDev = (await import('electron-is-dev')).default; | ||
if (!isDev) { | ||
const serve = (await import('electron-serve')).default; | ||
appServe = serve({ | ||
directory: path.join(__dirname, 'out'), | ||
}); | ||
} | ||
}; | ||
|
||
const createWindow = async () => { | ||
const win = new BrowserWindow({ | ||
width: 1200, | ||
height: 800, | ||
webPreferences: { | ||
preload: path.join(__dirname, 'preload.js'), | ||
}, | ||
}); | ||
|
||
if (!isDev && appServe) { | ||
try { | ||
await appServe(win); | ||
win.loadURL('app://-'); | ||
} catch (err) { | ||
console.error('Failed to serve app:', err); | ||
win.loadFile(path.join(__dirname, 'out/index.html')); | ||
} | ||
} else { | ||
win.loadFile(path.join(__dirname, 'out/index.html')).catch((err) => { | ||
console.error('Failed to load local file:', err); | ||
win.loadURL('http://editor.casbin.org'); | ||
}); | ||
|
||
win.webContents.on('did-fail-load', async () => { | ||
try { | ||
await win.loadFile(path.join(__dirname, 'out/index.html')); | ||
} catch (err) { | ||
console.error('Retry loading local file failed:', err); | ||
win.loadURL('http://editor.casbin.org'); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
app.whenReady().then(async () => { | ||
await loadModules(); | ||
createWindow(); | ||
}); | ||
|
||
app.on('window-all-closed', () => { | ||
if (process.platform !== 'darwin') { | ||
app.quit(); | ||
} | ||
}); | ||
|
||
app.on('activate', () => { | ||
if (BrowserWindow.getAllWindows().length === 0) { | ||
createWindow(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { contextBridge, ipcRenderer } = require('electron'); | ||
|
||
contextBridge.exposeInMainWorld('electronAPI', { | ||
on: (channel, callback) => { | ||
ipcRenderer.on(channel, callback); | ||
}, | ||
send: (channel, args) => { | ||
ipcRenderer.send(channel, args); | ||
}, | ||
}); |
Oops, something went wrong.