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
2 changes: 2 additions & 0 deletions cypress/e2e/code-testing.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Tests fail sporadically. Better class stability and consistent loading timeframes should be built into notebook.
// For now, run enough times till you can see every test pass at least once.
describe('EditorJS Code Testing', function() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
2 changes: 2 additions & 0 deletions cypress/e2e/reload-testing.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Tests fail sporadically. Better class stability and consistent loading timeframes should be built into notebook.
// For now, run enough times till you can see every test pass at least once.
describe('EditorJS Reload', function() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
4 changes: 2 additions & 2 deletions cypress/e2e/simple-editing.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Due to instabilities, this seems to occasionally fail.

// Tests fail sporadically. Better class stability and consistent loading timeframes should be built into notebook.
// For now, run enough times till you can see every test pass at least once.
describe('EditorJS interaction', function() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions src/app/notebook/export/export.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1 mat-dialog-title>Open Notebook</h1>
<div mat-dialog-content>
<button mat-menu-item (click)="export()">Export All</button>
<button mat-menu-item (click)="exportNotebook()">Export Current Notebook</button>
<button mat-menu-item (click)="destroy()">Delete All Data (caution)</button>
</div>
<div mat-dialog-actions>
<button mat-button (click)="closeDialog()">Close</button>
</div>
23 changes: 23 additions & 0 deletions src/app/notebook/export/export.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ExportComponent } from './export.component';

describe('ExportComponent', () => {
let component: ExportComponent;
let fixture: ComponentFixture<ExportComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ExportComponent ]
})
.compileComponents();

fixture = TestBed.createComponent(ExportComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
32 changes: 32 additions & 0 deletions src/app/notebook/export/export.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Component} from "@angular/core";
import {MatDialog} from "@angular/material/dialog";
import * as url from "../shell/url";
import {DatabaseManager} from "../shell/DatabaseManager";

@Component({
selector: 'app-exporter',
templateUrl: './export.component.html',
styleUrls: ['./export.component.css']
})
export class ExportComponent {
channel = new MessageChannel();
constructor(private dialog: MatDialog, private manager: DatabaseManager) {
}

closeDialog() {
this.dialog.closeAll();
}

async export() {
console.log(await this.manager.exportDatabase());
}

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

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

}
1 change: 1 addition & 0 deletions src/app/notebook/history.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class HistoryComponent implements OnInit {
);
}

// t stands for topic, p stands peer, n stands for titles
getNotebookLocation(item: any) {
return `${location.origin}?t=${item.topic}&p=${url.read('p')}&n=${item.title}`
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/notebook/notebook.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {HistoryComponent} from "./history.component";
import {DatabaseManager} from "./shell/DatabaseManager";
import {transformBulkEditorChanges} from "./transform-bulk-editor-changes";
import { OpenComponent } from './open/open.component';
import { ExportComponent } from './export/export.component';

function readAsDataURL(file: File): Promise<string> {
if (!file) {
Expand Down Expand Up @@ -243,7 +244,7 @@ export class NotebookComponent implements OnInit {
}

exportNotebook() {
window.dispatchEvent(new CustomEvent('shell.ExportNotebook'));
this.dialog.open(ExportComponent);
}

saveInUrl() {
Expand Down
4 changes: 3 additions & 1 deletion src/app/notebook/notebook.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { ClipboardModule } from '@angular/cdk/clipboard';
import {HistoryComponent} from "./history.component";
import { AssistantComponent } from './assistant/assistant.component';
import { OpenComponent } from './open/open.component';
import { ExportComponent } from './export/export.component';

@NgModule({
declarations: [
Expand All @@ -33,7 +34,8 @@ import { OpenComponent } from './open/open.component';
ShareDialogComponent,
HistoryComponent,
AssistantComponent,
OpenComponent
OpenComponent,
ExportComponent
],
imports: [
CommonModule,
Expand Down
24 changes: 3 additions & 21 deletions src/app/notebook/open/open.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {Component, OnInit} from "@angular/core";
import {Component} from "@angular/core";
import {MatDialog} from "@angular/material/dialog";
import * as url from "../shell/url";
import {DatabaseManager} from "../shell/DatabaseManager";

@Component({
selector: 'app-opener',
templateUrl: './open.component.html',
styleUrls: ['./open.component.css']
})
export class OpenComponent implements OnInit {
export class OpenComponent {
channel = new MessageChannel();
constructor(private dialog: MatDialog, private manager: DatabaseManager) {
}
Expand All @@ -17,24 +16,6 @@ export class OpenComponent implements OnInit {
this.dialog.closeAll();
}

ngOnInit(): void {
this.manager.history$.subscribe((dataSource) =>
this.channel.port2.postMessage({
type: 'render',
displayedColumns: ['title', 'topic', 'Created At', 'Updated At', 'Created By', 'Last Edited By'],
dataSource
})
);
this.channel.port2.onmessage = (event: MessageEvent) => window.open(
this.getNotebookLocation(event.data.row), "_blank"
);
}


getNotebookLocation(item: any) {
return `${location.origin}?p=${url.read('p')}&n=${item.title}`
}

triggerFileInput() {
document.getElementById('notebook-file')?.click();
}
Expand All @@ -46,4 +27,5 @@ export class OpenComponent implements OnInit {
await this.manager.bulkInsertBlocks(blocks);
}
}

}
26 changes: 22 additions & 4 deletions src/app/notebook/shell/DatabaseManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
BehaviorSubject,
filter,
first,
firstValueFrom,
map,
Observable,
Expand Down Expand Up @@ -352,9 +351,28 @@ export class DatabaseManager {
return this._database?.exportJSON();
}

async exportCurrentNotebook() {
// @ts-ignore
return
async exportNotebook(peer: string) {
const data = await this._database?.exportJSON();
let toExport: Array<BlockDocument> = [];
data?.collections[1].docs.forEach((element) => {
if (element.createdBy === peer) {
toExport.push(element)
}
})
toExport.sort((element1, element2) => (element1.index ?? 0) - (element2.index ?? 0));
toExport = toExport.map((value) => {
const propertiesToDelete = [
'crdts', '_deleted', '_meta', "_rev", "_attachments"
];
const anyValue = value as any;
propertiesToDelete.forEach(property => {
if (property in value) {
delete anyValue[property];
}
});
return anyValue;
});
return {"blocks": toExport};
}

importDatabase(json: RxDumpDatabaseAny<RxCollection>) {
Expand Down
21 changes: 13 additions & 8 deletions src/app/notebook/shell/editorJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ export class EditorJS {
.exec()),
map(blocks => {
return blocks.map(doc => {
delete doc._data.crdts
delete doc._data._deleted
delete doc._data._deleted
delete doc._data._attachments
delete doc._data._rev
delete doc._data._meta
return doc._data
})
if (doc._data) {
const propertiesToDelete = [
Copy link
Owner

Choose a reason for hiding this comment

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

The variable should be declared outside of the loops.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, any suggested places for this constant?

'crdts', '_deleted', '_attachments', '_rev', '_meta'
];
propertiesToDelete.forEach(property => {
if (property in doc._data) {
delete doc._data[property];
}
});
return doc._data;
}
return doc;
});
}),
map(blocks => ({
version: EditorJS.version, blocks
Expand Down
21 changes: 2 additions & 19 deletions src/app/notebook/shell/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Shell {
private peerAddBlock = false;
private peerRemoveBlock = false;
private peerChangeBlock = false;
// environment is generally window
pranitsh marked this conversation as resolved.
Show resolved Hide resolved
constructor(private editor: EditorJS, private environment: any, private databaseManager: DatabaseManager) {
environment.webWorkers = this.jobs;
environment.downloadBlob = downloadBlob;
Expand Down Expand Up @@ -70,16 +71,6 @@ export class Shell {
window.dispatchEvent(new CustomEvent('shell.StopAll'));
this.databaseManager.removeAllBlocks().then();
});
environment.addEventListener('shell.ExportNotebook', (event: CustomEvent) => {
// @ts-ignore
globalThis.editor.save()?.then((data) => {
downloadFile([JSON.stringify(data)], {type: 'application/json', filename: `${url.read('n', 'EvaNotebook')}.json`});
event.detail?.port?.postMessage({
event: event.detail?.payload?.event, payload: data
});
}
);
});
environment.addEventListener('shell.DownloadFile', (event: CustomEvent) => {
downloadFile(event.detail.payload.blobParts, event.detail.payload.options);
});
Expand All @@ -105,31 +96,25 @@ export class Shell {
block?.call('dispatchShellStop');
}
});
//@ts-ignore
environment.addEventListener('localecho.println', (event: CustomEvent) => {
this.editor.blocks.getById(event.detail.payload.threadId)?.call('println', event.detail.payload.text);
});
//@ts-ignore
environment.addEventListener('shell.RequestCanvas', (event: CustomEvent) => {
this.editor.blocks.getById(event.detail.payload.threadId)?.call('transferControlToOffscreen');
});
//@ts-ignore
environment.addEventListener('shell.RequestCaptureStream', (event: CustomEvent) => {
this.editor.blocks.getById(event.detail.payload.threadId)?.call('captureStream');
});
//@ts-ignore
environment.addEventListener('shell.transferStreamToOffscreen', (event: CustomEvent) => {
this.jobs.get(event.detail.payload.threadId)?.worker?.postMessage({
event: 'transferStreamToOffscreen', payload: {
message: event.detail.payload.message
}
}, [event.detail.payload.message]);
});
//@ts-ignore
environment.addEventListener('table', (event: CustomEvent) => {
this.editor.blocks.getById(event.detail.payload.threadId)?.call('createTable');
});
//@ts-ignore
environment.addEventListener('tree', (event: CustomEvent) => {
this.editor.blocks.getById(event.detail.payload.threadId)?.call('createTree');
});
Expand Down Expand Up @@ -385,12 +370,10 @@ export class Shell {
this.editor.blocks.update(block.id, block.data);
}

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 change this to async if you don't require await?

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, kinda struggling to figure out what this is referencing. Could you please clarify which async?

private handleKeyPress(event: KeyboardEvent) {
private async handleKeyPress(event: KeyboardEvent) {
if (event.ctrlKey && event.key === 's') {
event.preventDefault();
this.databaseManager.saveInUrl();
}
}


}