-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
c636148
03bb4f1
2effb9f
0c7ba3b
b9258d6
988e81f
eaa74a3
4c8325d
cb855ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||
|
@@ -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); | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you do with the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?? ""}`; | ||
} | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the clarification! |
||
toExport.push(element) | ||
} | ||
}) | ||
|
@@ -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() { | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.