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

Looking for a new feature branch for exporting! #3

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/app/notebook/export/export.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class ExportComponent {
}

async exportNotebook() {
console.log(await this.manager.exportNotebook(url.read("p")));
console.log(await this.manager.exportNotebook(url.read("p"), url.read("t")));
}

async destroy() {
this.manager.destroy();
await this.manager.destroy();
}

}
19 changes: 16 additions & 3 deletions src/app/notebook/open/open.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {Component} from "@angular/core";
import {MatDialog} from "@angular/material/dialog";
import {DatabaseManager} from "../shell/DatabaseManager";
import * as url from "../shell/url";
import { BlockDocument } from "../shell/DatabaseManager";

@Component({
selector: 'app-opener',
Expand All @@ -23,9 +25,20 @@ export class OpenComponent {
async importNotebook(event: Event) {
const input = event.target as HTMLInputElement;
if (input.files && input.files.length > 0) {
const blocks = JSON.parse(await input.files[0].text()).blocks;
await this.manager.bulkInsertBlocks(blocks);
const currentBlocks = (await this.manager.exportNotebook(url.read("p"), url.read("t"))).blocks;
let currentMaxndex = currentBlocks.reduce((max, _, index) => Math.max(max, index), 0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unnecessary because you can use:
currentMaxIndex = currentBlocks.lenght-1;
Blocks have their own index because they are stored randomly in the database, but they are presented in order to the user. So, if you want to store new blocks after the current ones, you only need the number of blocks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard exactly to remember what the rationale behind this was, but I believe somehow the numbers didn't match, and I wanted to use something different and was experimenting with getting the correct numbers for the place to insert. Seems I will have to change my approach either way.

let newBlocks = JSON.parse(await input.files[0].text()).blocks;
newBlocks = newBlocks.map((block: BlockDocument) => {
if (block && block.index !== undefined) {
block.index = currentMaxndex;
currentMaxndex += 1;
}
return block;
}).filter((block: BlockDocument | undefined) => block !== undefined).forEach;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you do with the forEach?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, some of this is just me getting my thoughts together. Seems I missed that!

const item = await this.manager.bulkInsertBlocks(newBlocks, url.read("p"), url.read("t"));
if (item) {
window.location.href = `${location.origin}?t=${item.topic ?? ""}&p=${item.peer ?? ""}`;
}
}
}

}
41 changes: 24 additions & 17 deletions src/app/notebook/shell/DatabaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,21 @@ export class DatabaseManager {
);
}

bulkInsertBlocks(blocks: BlockDocument[]) {
blocks.forEach((block: BlockDocument, index: number) => {
block.index = index;
block.createdBy = this._uuid;
block.lastEditedBy = this._uuid;
block.topic = this.topic;
bulkInsertBlocks(blocks: BlockDocument[], peer: string | undefined = undefined, topic: string | undefined = undefined) {
if ((peer && !topic) || (!peer && topic)) {
return;
} else if (!peer && !topic) {
peer = this._uuid;
topic = this.topic;
}
blocks.forEach((block: BlockDocument) => {
block.createdBy = peer;
block.lastEditedBy = peer;
block.topic = topic;
});
// @ts-ignore
return this._database?.blocks.bulkInsert(blocks);
const db = this._database as any;
db.blocks.bulkInsert(blocks);
return {"peer": peer, "topic": topic}
}

decodeHtmlEntities(html: string) {
Expand Down Expand Up @@ -351,11 +357,11 @@ export class DatabaseManager {
return this._database?.exportJSON();
}

async exportNotebook(peer: string) {
async exportNotebook(peer: string, topic: string) {
const data = await this._database?.exportJSON();
let toExport: Array<BlockDocument> = [];
data?.collections[1].docs.forEach((element) => {
if (element.createdBy === peer) {
if (element.createdBy === peer && element.topic === topic) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The topic is an identifier for a set of blocks intended to represent a notebook, so it should be

if (element.topic === topic)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarification!

toExport.push(element)
}
})
Expand Down Expand Up @@ -411,13 +417,14 @@ export class DatabaseManager {
}

async insert(name: string, outputData: OutputData) {
// @ts-ignore
return (await this._database[name].insertCRDT({
ifMatch: {
$set: outputData
},
}))._data;
// @ts-ignore
const db = this._database as any;
if (this._database && db[name]) {
return await db[name].insertCRDT({
ifMatch: {
$set: outputData
},
})._data;
}
}

async destroy() {
Expand Down
3 changes: 1 addition & 2 deletions src/app/notebook/shell/editorJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export class EditorJS {
public static version: "2.26.5";
public readonly blocks = new Blocks(this.environment);

constructor(private environment: Environment) {
}
constructor(private environment: Environment) {}

get isReady(): Promise<boolean> {
return new Promise((resolve) => resolve(true));
Expand Down