Skip to content

Commit

Permalink
✨ feat: add documents property
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Mar 17, 2024
1 parent 515d3a0 commit e9366e4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/chili-core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IApplication extends IPropertyChanged {
readonly services: IService[];
readonly storage: IStorage;
readonly views: ObservableCollection<IView>;
readonly documents: Set<IDocument>;
executingCommand: ICommand | undefined;
activeView: IView | undefined;
newDocument(name: string): Promise<IDocument>;
Expand Down
9 changes: 8 additions & 1 deletion packages/chili-ui/src/home/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ 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;
}

export const Home = async (app: IApplication) => {
let documentArray: RecentDocumentDTO[] = await app.storage.page(
Constants.DBName,
Expand Down Expand Up @@ -64,7 +71,7 @@ export const Home = async (app: IApplication) => {
{
className: style.document,
onclick: () => {
if (item.id === app.activeView?.document?.id) {
if (hasOpen(app, item.id)) {
PubSub.default.pub("displayHome", false);
} else {
PubSub.default.pub(
Expand Down
1 change: 1 addition & 0 deletions packages/chili/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class Application extends Observable implements IApplication {
}

readonly views = new ObservableCollection<IView>();
readonly documents: Set<IDocument> = new Set<IDocument>();

executingCommand: ICommand | undefined;

Expand Down
2 changes: 2 additions & 0 deletions packages/chili/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class Document extends Observable implements IDocument {
this.selection = new Selection(this);
PubSub.default.sub("nodeLinkedListChanged", this.handleModelChanged);
Logger.info(`new document: ${name}`);
application.documents.add(this);
}

private handleRootNodeNameChanged = (prop: string) => {
Expand Down Expand Up @@ -129,6 +130,7 @@ export class Document extends Observable implements IDocument {
let views = this.application.views.filter((x) => x.document === this);
this.application.views.remove(...views);
this.application.activeView = this.application.views.at(0);
this.application.documents.delete(this);

Logger.info(`document: ${this._name} closed`);
this.dispose();
Expand Down

0 comments on commit e9366e4

Please sign in to comment.