Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update title on note content change #143

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface INote {
v: 1;
uuid: string;
content: string;
title: string;
style: Style;
tags: ITag[];
modified: Date;
Expand Down Expand Up @@ -151,6 +152,7 @@ export class Note extends GObject.Object {
v: 1;
uuid: string;
content: string;
title: string;
style: Style;
tag_list: Gio.ListStore<Tag>;
modified: GLib.DateTime;
Expand Down Expand Up @@ -201,6 +203,7 @@ export class Note extends GObject.Object {
this.v = note.v;
this.uuid = note.uuid;
this.content = note.content;
this.title = note.title;
this.style = note.style;
this.tag_list = Gio.ListStore.new(Tag.$gtype) as Gio.ListStore<Tag>;
this.tags = note.tags;
Expand All @@ -211,13 +214,31 @@ export class Note extends GObject.Object {
this.width = note.width;
this.height = note.height;
this.open = note.open ?? false;

// @ts-expect-error incorrect types
this.bind_property_full(
"content",
this,
"title",
GObject.BindingFlags.SYNC_CREATE,
(_, content) => {
if (!content) return [false, ""];

const title = content.split("\n")[0];
return [true, title.length > 20 ? title.slice(0, 20) + "..." : title];
},
anig1scur marked this conversation as resolved.
Show resolved Hide resolved
null
);


}

static generate() {
return new this({
v: 1,
uuid: GLib.uuid_string_random(),
content: "",
title: "",
style: SETTINGS.DEFAULT_STYLE,
tags: [],
modified: new Date(),
Expand All @@ -232,6 +253,7 @@ export class Note extends GObject.Object {
v: this.v,
uuid: this.uuid,
content: this.content,
title: this.title,
style: this.style,
tags: this.tags.map((tag) => ({
name: tag.name,
Expand All @@ -250,6 +272,7 @@ export class Note extends GObject.Object {
v: this.v,
uuid: this.uuid,
content: this.content,
title: this.title,
style: this.style,
tags: this.tags,
modified: this.modified_date,
Expand All @@ -270,6 +293,8 @@ export class Note extends GObject.Object {
// deno-fmt-ignore
content: GObject.ParamSpec.string("content", "Content", "Content of the note", GObject.ParamFlags.READWRITE, ""),
// deno-fmt-ignore
title: GObject.ParamSpec.string("title", "Title", "Title of the note", GObject.ParamFlags.READWRITE, ""),
// deno-fmt-ignore
style: GObject.ParamSpec.int("style", "Style", "Style of the note", GObject.ParamFlags.READWRITE, 0, 100, 0),
// deno-fmt-ignore
tag_list: this.tag_list_pspec,
Expand Down
14 changes: 14 additions & 0 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export class Window extends Adw.ApplicationWindow {
this.default_width = note.width;
this.default_height = note.height;

this.update_title();

this.note.connect("notify::title", () => {
this.update_title();
});

this.connect("close-request", () => {
if (this.deleted) return;

Expand Down Expand Up @@ -151,6 +157,14 @@ export class Window extends Adw.ApplicationWindow {
popover.add_child(this.selector, "notestyleswitcher");
}

update_title() {
if (!this.note.title) {
this.set_title("Untitled Note");
return;
}
this.set_title(this.note.title);
}

last_revealer = false;

update_link(selected: boolean, text: string) {
Expand Down
Loading