mirror of
https://github.com/JannisX11/blockbench.git
synced 2025-04-06 17:31:09 +08:00
Vite conditional import
This commit is contained in:
parent
c9426c4465
commit
ab74a0271d
@ -317,7 +317,7 @@
|
||||
color: var(--color-light);
|
||||
}
|
||||
#display_bar .tool, #display_ref_bar > div {
|
||||
width: calc(100% / 8 - 2px);
|
||||
width: calc((100% * 0.125) - 2px);
|
||||
max-width: 52px;
|
||||
}
|
||||
#display_ref_bar > div > label {
|
||||
|
@ -27,8 +27,6 @@
|
||||
<body spellcheck="false" class="maximized">
|
||||
<script>
|
||||
if (typeof module === 'object') {window.module = module; module = undefined;}//jQuery Fix
|
||||
const isApp = typeof require !== 'undefined';
|
||||
|
||||
|
||||
if (localStorage.getItem('theme')) {
|
||||
try {
|
||||
|
@ -1,5 +1,3 @@
|
||||
import {version} from '../package.json';
|
||||
const appVersion = version;
|
||||
|
||||
export const LastVersion = localStorage.getItem('last_version') || localStorage.getItem('welcomed_version') || appVersion;
|
||||
|
||||
@ -413,6 +411,5 @@ export const StateMemory = {
|
||||
Object.assign(window, {
|
||||
LastVersion,
|
||||
Blockbench,
|
||||
StateMemory,
|
||||
appVersion
|
||||
StateMemory
|
||||
});
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { Blockbench } from "./api";
|
||||
import { ipcRenderer } from "./desktop";
|
||||
import { ipcRenderer } from "./desktop.electron";
|
||||
import { loadInstalledPlugins } from "./plugin_loader";
|
||||
import { animate } from "./preview/preview";
|
||||
import { initializeWebApp, loadInfoFromURL } from "./web";
|
||||
import { initializeWebApp, loadInfoFromURL } from "./web.browser";
|
||||
|
||||
Interface.page_wrapper = document.getElementById('page_wrapper');
|
||||
Interface.work_screen = document.getElementById('work_screen');
|
||||
|
@ -58,8 +58,8 @@ import "./interface/action_control.js"
|
||||
import "./copy_paste.js"
|
||||
import "./undo.js"
|
||||
|
||||
import './desktop.js'
|
||||
//import './web.js'
|
||||
import './desktop.electron'
|
||||
import './web.browser'
|
||||
|
||||
import "./edit_sessions.js"
|
||||
import "./validator.js"
|
||||
|
@ -1,160 +1,160 @@
|
||||
Blockbench.queries = {};
|
||||
(function() {
|
||||
let query_string = location.search || location.hash;
|
||||
if (query_string) {
|
||||
query_string = decodeURIComponent(query_string.substring(1));
|
||||
let queries = query_string.split('&');
|
||||
queries.forEach(string => {
|
||||
let [key, value] = string.split(/=\s*(.+)/);
|
||||
Blockbench.queries[key] = value || true;
|
||||
})
|
||||
}
|
||||
})()
|
||||
|
||||
export function initializeWebApp() {
|
||||
|
||||
$(document.body).on('click', 'a[href]', (event) => {
|
||||
event.preventDefault();
|
||||
window.open(event.currentTarget.href, '_blank');
|
||||
});
|
||||
if (location.host == 'blockbench-dev.netlify.app') {
|
||||
let button = $(`<a href="https://www.netlify.com/" style="padding: 10px; color: white; cursor: pointer; text-decoration: none; display: block;" target="_blank" rel="noopener">
|
||||
Hosted by
|
||||
<img src="./assets/netlify-full-logo-dark.svg" height="20px" style="vertical-align: text-top;">
|
||||
</div>`);
|
||||
button.insertBefore('#start_files');
|
||||
}
|
||||
if (!Blockbench.isTouch && !Blockbench.isPWA) {
|
||||
$('#web_download_button').show()
|
||||
}
|
||||
|
||||
if (Blockbench.browser == 'firefox') {
|
||||
document.body.style.imageRendering = 'crisp-edges'
|
||||
}
|
||||
}
|
||||
addEventListener('load', function() {
|
||||
window.history.pushState({}, '')
|
||||
})
|
||||
addEventListener('popstate', e => {
|
||||
if (ModelProject.all.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (open_interface) {
|
||||
if (typeof open_interface.cancel == 'function') {
|
||||
open_interface.cancel(event);
|
||||
} else if (typeof open_interface == 'string' && open_dialog) {
|
||||
$('dialog#'+open_dialog).find('.cancel_btn:not([disabled])').trigger('click');
|
||||
}
|
||||
|
||||
} else if (Interface.tab_bar.new_tab.visible) {
|
||||
Interface.tab_bar.new_tab.close()
|
||||
|
||||
} else if (open_menu) {
|
||||
open_menu.hide()
|
||||
|
||||
} else if (Undo && Undo.index) {
|
||||
Undo.undo()
|
||||
|
||||
} else if (!Blockbench.isMobile) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.history.pushState({}, '');
|
||||
})
|
||||
|
||||
try {
|
||||
window.matchMedia('(display-mode: standalone)').addEventListener('change', (evt) => {
|
||||
if (!Blockbench.isMobile) $('#web_download_button').toggle(!evt.matches);
|
||||
});
|
||||
} catch (err) {
|
||||
if (!Blockbench.isMobile) $('#web_download_button').hide();
|
||||
}
|
||||
|
||||
export async function loadInfoFromURL() {
|
||||
if (Blockbench.queries.session) {
|
||||
EditSession.token = Blockbench.queries.session;
|
||||
BarItems.edit_session.click();
|
||||
}
|
||||
|
||||
if (Blockbench.queries.plugins) {
|
||||
let plugin_ids = Blockbench.queries.plugins.split(/,/);
|
||||
let plugins = plugin_ids.map(id => Plugins.all.find(plugin => plugin.id == id))
|
||||
.filter(p => p instanceof Plugin && p.installed == false && p.isInstallable() == true);
|
||||
if (plugins.length) {
|
||||
await new Promise(resolve => {
|
||||
let form = {
|
||||
info: {type: 'info', text: 'dialog.load_plugins_from_query.text'}
|
||||
}
|
||||
plugins.forEach(plugin => {
|
||||
form[plugin.id.replace(/\./g, '_')] = {type: 'checkbox', label: plugin.name, description: plugin.description, value: true}
|
||||
})
|
||||
new Dialog({
|
||||
id: 'load_plugins_from_query',
|
||||
title: 'dialog.load_plugins_from_query.title',
|
||||
form,
|
||||
buttons: ['dialog.plugins.install', 'dialog.cancel'],
|
||||
onConfirm: async function(result) {
|
||||
let promises = [];
|
||||
plugins.forEach(plugin => {
|
||||
if (result[plugin.id.replace(/\./g, '_')]) {
|
||||
promises.push(plugin.download());
|
||||
}
|
||||
})
|
||||
await Promise.all(promises);
|
||||
resolve();
|
||||
},
|
||||
onCancel() {
|
||||
resolve();
|
||||
}
|
||||
}).show();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (Blockbench.queries.m) {
|
||||
$.getJSON(`https://blckbn.ch/api/models/${Blockbench.queries.m}`, (model, b) => {
|
||||
Codecs.project.load(model, {path: ''});
|
||||
}).fail(() => {
|
||||
Blockbench.showMessageBox({
|
||||
title: 'message.invalid_link',
|
||||
message: tl('message.invalid_link.message', ['`'+Blockbench.queries.m+'`']),
|
||||
icon: 'running_with_errors'
|
||||
})
|
||||
})
|
||||
} else if (Blockbench.queries.loadtype) {
|
||||
let file = {
|
||||
content: Blockbench.queries.loaddata,
|
||||
name: Blockbench.queries.loadname || 'file',
|
||||
path: Blockbench.queries.loadname || 'file'
|
||||
};
|
||||
switch (Blockbench.queries.loadtype) {
|
||||
case 'minecraft_skin': {
|
||||
Formats.skin.setup_dialog.show();
|
||||
Formats.skin.setup_dialog.setFormValues({
|
||||
texture: file
|
||||
})
|
||||
break;
|
||||
}
|
||||
case 'image': {
|
||||
loadImages([file]);
|
||||
break;
|
||||
}
|
||||
case 'json': {
|
||||
loadModelFile(file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Misc
|
||||
window.onbeforeunload = function() {
|
||||
let unsaved_projects = ModelProject.all.find(project => !project.saved);
|
||||
if (unsaved_projects) {
|
||||
return 'Unsaved Changes';
|
||||
} else {
|
||||
Blockbench.dispatchEvent('before_closing')
|
||||
if (Project.EditSession) Project.EditSession.quit()
|
||||
}
|
||||
}
|
||||
Blockbench.queries = {};
|
||||
(function() {
|
||||
let query_string = location.search || location.hash;
|
||||
if (query_string) {
|
||||
query_string = decodeURIComponent(query_string.substring(1));
|
||||
let queries = query_string.split('&');
|
||||
queries.forEach(string => {
|
||||
let [key, value] = string.split(/=\s*(.+)/);
|
||||
Blockbench.queries[key] = value || true;
|
||||
})
|
||||
}
|
||||
})()
|
||||
|
||||
export function initializeWebApp() {
|
||||
|
||||
$(document.body).on('click', 'a[href]', (event) => {
|
||||
event.preventDefault();
|
||||
window.open(event.currentTarget.href, '_blank');
|
||||
});
|
||||
if (location.host == 'blockbench-dev.netlify.app') {
|
||||
let button = $(`<a href="https://www.netlify.com/" style="padding: 10px; color: white; cursor: pointer; text-decoration: none; display: block;" target="_blank" rel="noopener">
|
||||
Hosted by
|
||||
<img src="./assets/netlify-full-logo-dark.svg" height="20px" style="vertical-align: text-top;">
|
||||
</div>`);
|
||||
button.insertBefore('#start_files');
|
||||
}
|
||||
if (!Blockbench.isTouch && !Blockbench.isPWA) {
|
||||
$('#web_download_button').show()
|
||||
}
|
||||
|
||||
if (Blockbench.browser == 'firefox') {
|
||||
document.body.style.imageRendering = 'crisp-edges'
|
||||
}
|
||||
}
|
||||
addEventListener('load', function() {
|
||||
window.history.pushState({}, '')
|
||||
})
|
||||
addEventListener('popstate', e => {
|
||||
if (ModelProject.all.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (open_interface) {
|
||||
if (typeof open_interface.cancel == 'function') {
|
||||
open_interface.cancel(event);
|
||||
} else if (typeof open_interface == 'string' && open_dialog) {
|
||||
$('dialog#'+open_dialog).find('.cancel_btn:not([disabled])').trigger('click');
|
||||
}
|
||||
|
||||
} else if (Interface.tab_bar.new_tab.visible) {
|
||||
Interface.tab_bar.new_tab.close()
|
||||
|
||||
} else if (open_menu) {
|
||||
open_menu.hide()
|
||||
|
||||
} else if (Undo && Undo.index) {
|
||||
Undo.undo()
|
||||
|
||||
} else if (!Blockbench.isMobile) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.history.pushState({}, '');
|
||||
})
|
||||
|
||||
try {
|
||||
window.matchMedia('(display-mode: standalone)').addEventListener('change', (evt) => {
|
||||
if (!Blockbench.isMobile) $('#web_download_button').toggle(!evt.matches);
|
||||
});
|
||||
} catch (err) {
|
||||
if (!Blockbench.isMobile) $('#web_download_button').hide();
|
||||
}
|
||||
|
||||
export async function loadInfoFromURL() {
|
||||
if (Blockbench.queries.session) {
|
||||
EditSession.token = Blockbench.queries.session;
|
||||
BarItems.edit_session.click();
|
||||
}
|
||||
|
||||
if (Blockbench.queries.plugins) {
|
||||
let plugin_ids = Blockbench.queries.plugins.split(/,/);
|
||||
let plugins = plugin_ids.map(id => Plugins.all.find(plugin => plugin.id == id))
|
||||
.filter(p => p instanceof Plugin && p.installed == false && p.isInstallable() == true);
|
||||
if (plugins.length) {
|
||||
await new Promise(resolve => {
|
||||
let form = {
|
||||
info: {type: 'info', text: 'dialog.load_plugins_from_query.text'}
|
||||
}
|
||||
plugins.forEach(plugin => {
|
||||
form[plugin.id.replace(/\./g, '_')] = {type: 'checkbox', label: plugin.name, description: plugin.description, value: true}
|
||||
})
|
||||
new Dialog({
|
||||
id: 'load_plugins_from_query',
|
||||
title: 'dialog.load_plugins_from_query.title',
|
||||
form,
|
||||
buttons: ['dialog.plugins.install', 'dialog.cancel'],
|
||||
onConfirm: async function(result) {
|
||||
let promises = [];
|
||||
plugins.forEach(plugin => {
|
||||
if (result[plugin.id.replace(/\./g, '_')]) {
|
||||
promises.push(plugin.download());
|
||||
}
|
||||
})
|
||||
await Promise.all(promises);
|
||||
resolve();
|
||||
},
|
||||
onCancel() {
|
||||
resolve();
|
||||
}
|
||||
}).show();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (Blockbench.queries.m) {
|
||||
$.getJSON(`https://blckbn.ch/api/models/${Blockbench.queries.m}`, (model, b) => {
|
||||
Codecs.project.load(model, {path: ''});
|
||||
}).fail(() => {
|
||||
Blockbench.showMessageBox({
|
||||
title: 'message.invalid_link',
|
||||
message: tl('message.invalid_link.message', ['`'+Blockbench.queries.m+'`']),
|
||||
icon: 'running_with_errors'
|
||||
})
|
||||
})
|
||||
} else if (Blockbench.queries.loadtype) {
|
||||
let file = {
|
||||
content: Blockbench.queries.loaddata,
|
||||
name: Blockbench.queries.loadname || 'file',
|
||||
path: Blockbench.queries.loadname || 'file'
|
||||
};
|
||||
switch (Blockbench.queries.loadtype) {
|
||||
case 'minecraft_skin': {
|
||||
Formats.skin.setup_dialog.show();
|
||||
Formats.skin.setup_dialog.setFormValues({
|
||||
texture: file
|
||||
})
|
||||
break;
|
||||
}
|
||||
case 'image': {
|
||||
loadImages([file]);
|
||||
break;
|
||||
}
|
||||
case 'json': {
|
||||
loadModelFile(file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Misc
|
||||
window.onbeforeunload = function() {
|
||||
let unsaved_projects = ModelProject.all.find(project => !project.saved);
|
||||
if (unsaved_projects) {
|
||||
return 'Unsaved Changes';
|
||||
} else {
|
||||
Blockbench.dispatchEvent('before_closing')
|
||||
if (Project.EditSession) Project.EditSession.quit()
|
||||
}
|
||||
}
|
32
package-lock.json
generated
32
package-lock.json
generated
@ -24,6 +24,7 @@
|
||||
"electron-builder": "^24.13.3",
|
||||
"typescript": "^5.8.2",
|
||||
"vite": "^6.2.0",
|
||||
"vite-plugin-conditional-import": "0.1.7",
|
||||
"vite-plugin-electron": "^0.29.0",
|
||||
"vite-plugin-electron-renderer": "^0.14.6",
|
||||
"vite-plugin-pwa": "^0.21.1",
|
||||
@ -4884,6 +4885,13 @@
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/es-module-lexer": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.5.0.tgz",
|
||||
"integrity": "sha512-pqrTKmwEIgafsYZAGw9kszYzmagcE/n4dbgwGWLEXg7J4QFJVQRBld8j3Q3GNez79jzxZshq0bcT962QHOghjw==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/es-to-primitive": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz",
|
||||
@ -7860,6 +7868,30 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/vite-plugin-conditional-import": {
|
||||
"version": "0.1.7",
|
||||
"resolved": "https://registry.npmjs.org/vite-plugin-conditional-import/-/vite-plugin-conditional-import-0.1.7.tgz",
|
||||
"integrity": "sha512-q1galZvTv2K0Sjo+41AlXIatqA5ai6njfU+vO4Gn399c5Q7skvNU250J5baQDNdRL6iOMl+IgfYGNMYN1n2rSA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"es-module-lexer": "1.5.0",
|
||||
"magic-string": "0.30.9"
|
||||
}
|
||||
},
|
||||
"node_modules/vite-plugin-conditional-import/node_modules/magic-string": {
|
||||
"version": "0.30.9",
|
||||
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.9.tgz",
|
||||
"integrity": "sha512-S1+hd+dIrC8EZqKyT9DstTH/0Z+f76kmmvZnkfQVmOpDEF9iVgdYif3Q/pIWHmCoo59bQVGW0kVL3e2nl+9+Sw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@jridgewell/sourcemap-codec": "^1.4.15"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/vite-plugin-electron": {
|
||||
"version": "0.29.0",
|
||||
"resolved": "https://registry.npmjs.org/vite-plugin-electron/-/vite-plugin-electron-0.29.0.tgz",
|
||||
|
@ -137,6 +137,7 @@
|
||||
"electron-builder": "^24.13.3",
|
||||
"typescript": "^5.8.2",
|
||||
"vite": "^6.2.0",
|
||||
"vite-plugin-conditional-import": "0.1.7",
|
||||
"vite-plugin-electron": "^0.29.0",
|
||||
"vite-plugin-electron-renderer": "^0.14.6",
|
||||
"vite-plugin-pwa": "^0.21.1",
|
||||
|
@ -1,6 +1,7 @@
|
||||
import fs from 'node:fs'
|
||||
import { defineConfig } from 'vite'
|
||||
import electron from 'vite-plugin-electron/simple'
|
||||
import conditionalImportPlugin from "vite-plugin-conditional-import";
|
||||
import pkg from './package.json'
|
||||
|
||||
// import vue from '@vitejs/plugin-vue'
|
||||
@ -17,8 +18,16 @@ export default defineConfig(({ command, mode }) => {
|
||||
build: {
|
||||
outDir: './dist-vite'
|
||||
},
|
||||
define: {
|
||||
isApp: true,
|
||||
appVersion: '"'+pkg.version+'"'
|
||||
},
|
||||
plugins: [
|
||||
// vue(),
|
||||
conditionalImportPlugin({
|
||||
currentEnv: "electron",
|
||||
envs: ["electron", "web"],
|
||||
}),
|
||||
electron({
|
||||
main: {
|
||||
// Shortcut of `build.lib.entry`
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import { VitePWA } from 'vite-plugin-pwa'
|
||||
import conditionalImportPlugin from "vite-plugin-conditional-import";
|
||||
import pkg from './package.json'
|
||||
|
||||
// import vue from '@vitejs/plugin-vue'
|
||||
@ -11,8 +12,16 @@ export default defineConfig(({ command, mode }) => {
|
||||
build: {
|
||||
outDir: './dist-vite'
|
||||
},
|
||||
define: {
|
||||
isApp: false,
|
||||
appVersion: '"'+pkg.version+'"'
|
||||
},
|
||||
plugins: [
|
||||
// vue(),
|
||||
conditionalImportPlugin({
|
||||
currentEnv: "browser",
|
||||
envs: ["electron", "browser"],
|
||||
}),
|
||||
VitePWA({
|
||||
registerType: 'autoUpdate',
|
||||
workbox: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user