Skip to content

Commit

Permalink
🦄 refactor: refactor Home
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Apr 11, 2024
1 parent e4e9816 commit 6e4c20d
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 123 deletions.
10 changes: 5 additions & 5 deletions packages/chili-builder/src/additionalModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import { CommandKeys, I18nKeys, Locale } from "chili-core";

export interface AdditionalCommand {
tabName: I18nKeys,
groupName: I18nKeys,
command: CommandKeys
tabName: I18nKeys;
groupName: I18nKeys;
command: CommandKeys;
}

export interface IAdditionalModule {
i18n(): Locale[];
commands(): AdditionalCommand[]
}
ribbonCommands(): AdditionalCommand[];
}
2 changes: 1 addition & 1 deletion packages/chili-builder/src/appBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class AppBuilder {
I18n.combineTranslation(local.code as any, local.translation);
});
if (this._window) {
module.commands().forEach((command) => {
module.ribbonCommands().forEach((command) => {
this._window!.registerRibbonCommand(command.tabName, command.groupName, command.command);
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/chili-core/src/ui/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import { Button } from "./button";

export interface IWindow {
init(app: IApplication): void;
registerHomeCommand(groupName: I18nKeys, command: CommandKeys | Button): void;
registerRibbonCommand(tabName: I18nKeys, groupName: I18nKeys, command: CommandKeys | Button): void;
}
}
9 changes: 5 additions & 4 deletions packages/chili-ui/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ export class Editor extends HTMLElement {
viewport,
),
new Statusbar().addClass(style.statusbar),
)
)
),
);
document.body.appendChild(this);
}

registerRibbonCommand(tabName: I18nKeys, groupName: I18nKeys, command: CommandKeys | Button) {
this.ribbonContent.ribbonTabs.find((p) => p.tabName === tabName)
this.ribbonContent.ribbonTabs
.find((p) => p.tabName === tabName)
?.groups.find((p) => p.groupName === groupName)
?.items.push(command);
}

}

customElements.define("chili-editor", Editor);
8 changes: 7 additions & 1 deletion packages/chili-ui/src/home/home.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
display: flex;
flex-direction: column;

& button {
.buttons {
display: flex;
flex-direction: column;
}

.button {
display: inline-block;
height: 48px;
width: auto;
font-size: 16px;
margin: 8px 32px;
padding: 0px 32px;
Expand Down
254 changes: 148 additions & 106 deletions packages/chili-ui/src/home/home.ts
Original file line number Diff line number Diff line change
@@ -1,123 +1,165 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { Constants, I18n, IApplication, ObservableCollection, PubSub, RecentDocumentDTO } from "chili-core";
import {
Constants,
I18n,
I18nKeys,
IApplication,
ObservableCollection,
PubSub,
RecentDocumentDTO,
} from "chili-core";
import { LanguageSelector } from "../components";
import { a, button, div, img, items, label, localize, span, svg } from "../controls";
import style from "./home.module.css";

function hasOpen(app: IApplication, documentId: string) {
for (const document of app.documents) {
if (document.id === documentId) return true;
}
return false;
interface ApplicationCommand {
display: I18nKeys;
icon?: string;
onclick: () => void;
}

export const Home = async (app: IApplication) => {
let documentArray: RecentDocumentDTO[] = await app.storage.page(
Constants.DBName,
Constants.RecentTable,
0,
);
let documents = new ObservableCollection(...documentArray);
return div(
{ className: style.root },
div(
{ className: style.left },
const applicationCommands = new ObservableCollection<ApplicationCommand>(
{
display: "command.document.new",
onclick: () => PubSub.default.pub("executeCommand", "doc.new"),
},
{
display: "command.document.open",
onclick: () => PubSub.default.pub("executeCommand", "doc.open"),
},
);

export class Home extends HTMLElement {
constructor(readonly app: IApplication) {
super();
this.className = style.root;
}

private hasOpen(documentId: string) {
for (const document of this.app.documents) {
if (document.id === documentId) return true;
}
return false;
}

private async getDocuments() {
let documentArray: RecentDocumentDTO[] = await this.app.storage.page(
Constants.DBName,
Constants.RecentTable,
0,
);
return new ObservableCollection(...documentArray);
}

async render() {
let documents = await this.getDocuments();
this.append(
div(
{ className: style.top },
{ className: style.left },
div(
{ className: style.logo },
svg({ icon: "icon-chili" }),
span({
textContent: "CHILI3D",
{ className: style.top },
div(
{ className: style.logo },
svg({ icon: "icon-chili" }),
span({
textContent: "CHILI3D",
}),
),
items({
className: style.buttons,
sources: applicationCommands,
template: (item) =>
button({
className: style.button,
textContent: localize(item.display),
onclick: item.onclick,
}),
}),
this.app.activeView?.document
? button({
className: `${style.button} ${style.back}`,
textContent: localize("common.back"),
onclick: () => {
PubSub.default.pub("displayHome", false);
},
})
: "",
),
div(
{ className: style.bottom },
a({
textContent: "Github",
href: "https://github.com/xiangechen/chili3d",
target: "_blank",
}),
),
button({
textContent: localize("command.document.new"),
onclick: () => PubSub.default.pub("executeCommand", "doc.new"),
}),
button({
textContent: localize("command.document.open"),
onclick: () => PubSub.default.pub("executeCommand", "doc.open"),
}),
app.activeView?.document
? button({
className: style.back,
textContent: localize("common.back"),
onclick: () => {
PubSub.default.pub("displayHome", false);
},
})
: "",
),
div(
{ className: style.bottom },
a({
textContent: "Github",
href: "https://github.com/xiangechen/chili3d",
target: "_blank",
}),
),
),
div(
{ className: style.right },
label({ className: style.welcome, textContent: localize("home.welcome") }),
div({ className: style.recent, textContent: localize("home.recent") }),
items({
className: style.documents,
sources: documents,
template: (item) =>
div(
{
className: style.document,
onclick: () => {
if (hasOpen(app, item.id)) {
PubSub.default.pub("displayHome", false);
} else {
PubSub.default.pub(
"showPermanent",
async () => {
let document = await app.openDocument(item.id);
document?.application.activeView?.cameraController.fitContent();
},
"toast.excuting{0}",
I18n.translate("command.document.open"),
);
}
},
},
img({ className: style.img, src: item.image }),
{ className: style.right },
label({ className: style.welcome, textContent: localize("home.welcome") }),
div({ className: style.recent, textContent: localize("home.recent") }),
items({
className: style.documents,
sources: documents,
template: (item) =>
div(
{ className: style.description },
span({ className: style.title, textContent: item.name }),
span({
className: style.date,
textContent: new Date(item.date).toLocaleDateString(),
{
className: style.document,
onclick: () => {
if (this.hasOpen(item.id)) {
PubSub.default.pub("displayHome", false);
} else {
PubSub.default.pub(
"showPermanent",
async () => {
let document = await this.app.openDocument(item.id);
document?.application.activeView?.cameraController.fitContent();
},
"toast.excuting{0}",
I18n.translate("command.document.open"),
);
}
},
},
img({ className: style.img, src: item.image }),
div(
{ className: style.description },
span({ className: style.title, textContent: item.name }),
span({
className: style.date,
textContent: new Date(item.date).toLocaleDateString(),
}),
),
svg({
className: style.delete,
icon: "icon-times",
onclick: async (e) => {
e.stopPropagation();
if (
window.confirm(I18n.translate("prompt.deleteDocument{0}", item.name))
) {
await this.app.storage.delete(
Constants.DBName,
Constants.DocumentTable,
item.id,
);
await this.app.storage.delete(
Constants.DBName,
Constants.RecentTable,
item.id,
);
documents.remove(item);
}
},
}),
),
svg({
className: style.delete,
icon: "icon-times",
onclick: async (e) => {
e.stopPropagation();
if (window.confirm(I18n.translate("prompt.deleteDocument{0}", item.name))) {
await app.storage.delete(
Constants.DBName,
Constants.DocumentTable,
item.id,
);
await app.storage.delete(
Constants.DBName,
Constants.RecentTable,
item.id,
);
documents.remove(item);
}
},
}),
),
}),
),
LanguageSelector({ className: style.language }),
);
};
}),
),
LanguageSelector({ className: style.language }),
);
document.body.appendChild(this);
}
}

customElements.define("chili-home", Home);
17 changes: 12 additions & 5 deletions packages/chili-ui/src/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ document.oncontextmenu = (e) => e.preventDefault();

export class MainWindow implements IWindow {
private _inited: boolean = false;
private _home?: HTMLElement;
private _home?: Home;
private _editor?: Editor;

constructor() {
Expand All @@ -25,6 +25,10 @@ export class MainWindow implements IWindow {
this._inited = true;
this._initHome(app);
this._initEditor(app);
this._initSubs(app);
}

private _initSubs(app: IApplication) {
const displayHome = debounce(this.displayHome, 100);
PubSub.default.sub("showToast", Toast.show);
PubSub.default.sub("showDialog", Dialog.show);
Expand All @@ -44,17 +48,20 @@ export class MainWindow implements IWindow {
};

private async _initHome(app: IApplication) {
this._home = await Home(app);
document.body.append(this._home);
this._home = new Home(app);
await this._home.render();
}

private async _initEditor(app: IApplication) {
this._editor = new Editor(app);
document.body.append(this._editor);
}

registerHomeCommand(groupName: I18nKeys, command: CommandKeys | Button): void {
throw new Error("Method not implemented.");
}

registerRibbonCommand(tabName: I18nKeys, groupName: I18nKeys, command: CommandKeys | Button) {
this._editor?.registerRibbonCommand(tabName, groupName, command)
this._editor?.registerRibbonCommand(tabName, groupName, command);
}

setTheme(theme: "light" | "dark") {
Expand Down

0 comments on commit 6e4c20d

Please sign in to comment.