diff --git a/packages/api/src/entity/highlight.ts b/packages/api/src/entity/highlight.ts index 6ef44d9af..dc9282190 100644 --- a/packages/api/src/entity/highlight.ts +++ b/packages/api/src/entity/highlight.ts @@ -3,12 +3,21 @@ import { CreateDateColumn, Entity, JoinColumn, + JoinTable, + ManyToMany, OneToOne, PrimaryGeneratedColumn, UpdateDateColumn, } from 'typeorm' -import { User } from './user' +import { Label } from './label' import { Page } from './page' +import { User } from './user' + +export enum HighlightType { + Highlight = 'HIGHLIGHT', + Redaction = 'REDACTION', // allowing people to remove text from the page + Note = 'NOTE', // allowing people to add a note at the document level +} @Entity({ name: 'highlight' }) export class Highlight { @@ -52,4 +61,30 @@ export class Highlight { @Column('timestamp') sharedAt?: Date + + @Column('real') + highlightPositionPercent!: number + + @Column('integer') + highlightPositionAnchorIndex!: number + + @Column('enum', { + enum: HighlightType, + default: HighlightType.Highlight, + }) + highlightType!: HighlightType + + @Column('text', { nullable: true }) + html?: string | null + + @Column('text', { nullable: true }) + color?: string | null + + @ManyToMany(() => Label, { eager: true }) + @JoinTable({ + name: 'entity_labels', + joinColumn: { name: 'highlight_id' }, + inverseJoinColumn: { name: 'label_id' }, + }) + labels?: Label[] } diff --git a/packages/api/src/entity/library_item.ts b/packages/api/src/entity/library_item.ts index bcb0d9ca4..406e48945 100644 --- a/packages/api/src/entity/library_item.ts +++ b/packages/api/src/entity/library_item.ts @@ -165,7 +165,11 @@ export class LibraryItem { subscription?: Subscription @ManyToMany(() => Label, { eager: true }) - @JoinTable() + @JoinTable({ + name: 'entity_labels', + joinColumn: { name: 'library_item_id' }, + inverseJoinColumn: { name: 'label_id' }, + }) labels?: Label[] @Column('enum', { enum: DirectionalityType, default: DirectionalityType.LTR }) diff --git a/packages/db/migrations/0119.do.entity_labels.sql b/packages/db/migrations/0119.do.entity_labels.sql new file mode 100755 index 000000000..cae9aa63a --- /dev/null +++ b/packages/db/migrations/0119.do.entity_labels.sql @@ -0,0 +1,14 @@ +-- Type: DO +-- Name: entity_labels +-- Description: Create table entity_labels + +BEGIN; + +CREATE TABLE omnivore.entity_labels ( + library_item_id uuid REFERENCES omnivore.library_item(id) ON DELETE CASCADE, + highlight_id uuid REFERENCES omnivore.highlight(id) ON DELETE CASCADE, + label_id uuid NOT NULL REFERENCES omnivore.labels(id) ON DELETE CASCADE, + PRIMARY KEY (library_item_id, highlight_id, label_id) +); + +COMMIT; diff --git a/packages/db/migrations/0119.do.library_item_labels.sql b/packages/db/migrations/0119.do.library_item_labels.sql deleted file mode 100755 index f9bcba00a..000000000 --- a/packages/db/migrations/0119.do.library_item_labels.sql +++ /dev/null @@ -1,13 +0,0 @@ --- Type: DO --- Name: library_item_labels --- Description: Create table library_item_labels - -BEGIN; - -CREATE TABLE omnivore.library_item_labels ( - library_item_id uuid NOT NULL REFERENCES omnivore.library_item(id) ON DELETE CASCADE, - label_id uuid NOT NULL REFERENCES omnivore.labels(id) ON DELETE CASCADE, - PRIMARY KEY (library_item_id, label_id) -); - -COMMIT; diff --git a/packages/db/migrations/0119.undo.entity_labels.sql b/packages/db/migrations/0119.undo.entity_labels.sql new file mode 100755 index 000000000..35d1f1576 --- /dev/null +++ b/packages/db/migrations/0119.undo.entity_labels.sql @@ -0,0 +1,9 @@ +-- Type: UNDO +-- Name: entity_labels +-- Description: Create table entity_labels + +BEGIN; + +DROP TABLE omnivore.entity_labels; + +COMMIT; diff --git a/packages/db/migrations/0119.undo.library_item_labels.sql b/packages/db/migrations/0119.undo.library_item_labels.sql deleted file mode 100755 index ec7ac7cb7..000000000 --- a/packages/db/migrations/0119.undo.library_item_labels.sql +++ /dev/null @@ -1,9 +0,0 @@ --- Type: UNDO --- Name: library_item_labels --- Description: Create table library_item_labels - -BEGIN; - -DROP TABLE omnivore.library_item_labels; - -COMMIT; diff --git a/packages/db/migrations/0121.do.update_highlight.sql b/packages/db/migrations/0121.do.update_highlight.sql new file mode 100755 index 000000000..111b9eb41 --- /dev/null +++ b/packages/db/migrations/0121.do.update_highlight.sql @@ -0,0 +1,20 @@ +-- Type: DO +-- Name: update_highlight +-- Description: Add fields to highlight table + +BEGIN; + +CREATE TYPE highlight_type AS ENUM ( + 'HIGHLIGHT', + 'REDACTION', + 'NOTE' +); + +ALTER TABLE omnivore.highlight + ADD COLUMN highlight_position_percent real NOT NULL DEFAULT 0, + ADD COLUMN highlight_position_anchor_index integer NOT NULL DEFAULT 0, + ADD COLUMN highlight_type highlight_type NOT NULL DEFAULT 'HIGHLIGHT', + ADD COLUMN color text, + ADD COLUMN html text; + +COMMIT; diff --git a/packages/db/migrations/0121.undo.update_highlight.sql b/packages/db/migrations/0121.undo.update_highlight.sql new file mode 100755 index 000000000..a223f8680 --- /dev/null +++ b/packages/db/migrations/0121.undo.update_highlight.sql @@ -0,0 +1,16 @@ +-- Type: UNDO +-- Name: update_highlight +-- Description: Add fields to highlight table + +BEGIN; + +ALTER TABLE omnivore.highlight + DROP COLUMN html, + DROP COLUMN color, + DROP COLUMN highlight_type, + DROP COLUMN highlight_position_anchor_index, + DROP COLUMN highlight_position_percent; + +DROP TYPE highlight_type; + +COMMIT;