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

Added automatic bullet point "- " when a list is made #107

Merged
merged 6 commits into from
Jun 1, 2024
Merged
Changes from all 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
53 changes: 53 additions & 0 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ export class WriteableStickyNote extends AbstractStickyNote {
constructor(note?: Note) {
super(note);

this.buffer.connect_after('insert-text', (buffer, loc, text, length) => {
this.on_text_inserted(buffer, loc, text, length);
});

this.buffer.connect("changed", () => {
if (this.updating) {
this.updating = false;
Expand Down Expand Up @@ -472,6 +476,55 @@ export class WriteableStickyNote extends AbstractStickyNote {
if (compare_tags(tags, this.note!.tags)) return;
this.change("tags", tags);
}

on_text_inserted(buffer: Gtk.TextBuffer, loc: Gtk.TextIter, text: string, length: number) {
if (text === '\n') {
const start_iter = loc.copy();
start_iter.backward_char();
start_iter.set_line_offset(0);

const end_iter = start_iter.copy();
end_iter.forward_chars(2);
const chars = buffer.get_text(start_iter, end_iter, false);

const simple_regex_pattern = /^[-+*] $/;
if (simple_regex_pattern.test(chars)) {
const bullet = chars[0];
const line_end = loc.copy();
line_end.backward_char();
if (line_end.get_line_offset() === 2) {
start_iter.set_line_offset(0);
buffer.delete(start_iter, loc);
} else {
buffer.insert(loc, bullet + ' ', -1);
}
} else{
const search_limit = start_iter.copy();
const search_end = start_iter.copy();
search_limit.forward_chars(10);

search_end.forward_find_char((ch) => {return ch === ' '}, search_limit);
search_end.forward_char()
const chars = buffer.get_text(start_iter, search_end, false);

const ordered_regex_pattern = /^\d+\. $/;
if (ordered_regex_pattern.test(chars)) {
const current_order = parseInt(chars.slice(0, -2));
const new_order = current_order + 1;
const new_order_bullet = `${new_order}. `;
const line_end = loc.copy();
line_end.backward_char();

if (line_end.get_line_offset() === current_order.toString().length + 2) {
start_iter.set_line_offset(0);
buffer.delete(start_iter, loc);
} else {
buffer.insert(loc, new_order_bullet, -1);
}
}
}
}
}
}

const compare_tags = (a: ITag[], b: ITag[]) => {
Expand Down