feat: add models package

This commit is contained in:
Karol Sójko
2022-07-05 20:47:11 +02:00
parent 60d1554ff7
commit b614c71e79
199 changed files with 8772 additions and 22 deletions

View File

@@ -0,0 +1,42 @@
import { createNote } from './../../Utilities/Test/SpecUtils'
describe('SNNote Tests', () => {
it('should safely type required fields of Note when creating from PayloadContent', () => {
const note = createNote({
title: 'Expected string',
text: ['unexpected array'] as never,
preview_plain: 'Expected preview',
preview_html: {} as never,
hidePreview: 'string' as never,
})
expect([
typeof note.title,
typeof note.text,
typeof note.preview_html,
typeof note.preview_plain,
typeof note.hidePreview,
]).toStrictEqual(['string', 'string', 'string', 'string', 'boolean'])
})
it('should preserve falsy values when casting from PayloadContent', () => {
const note = createNote({
preview_plain: null as never,
preview_html: undefined,
})
expect(note.preview_plain).toBeFalsy()
expect(note.preview_html).toBeFalsy()
})
it('should set mobilePrefersPlainEditor when given a valid choice', () => {
const selected = createNote({
mobilePrefersPlainEditor: true,
})
const unselected = createNote()
expect(selected.mobilePrefersPlainEditor).toBeTruthy()
expect(unselected.mobilePrefersPlainEditor).toBe(undefined)
})
})

View File

@@ -0,0 +1,34 @@
import { ContentType } from '@standardnotes/common'
import { DecryptedItem } from '../../Abstract/Item/Implementations/DecryptedItem'
import { ItemInterface } from '../../Abstract/Item/Interfaces/ItemInterface'
import { AppDataField } from '../../Abstract/Item/Types/AppDataField'
import { DecryptedPayloadInterface } from '../../Abstract/Payload/Interfaces/DecryptedPayload'
import { NoteContent, NoteContentSpecialized } from './NoteContent'
export const isNote = (x: ItemInterface): x is SNNote => x.content_type === ContentType.Note
export class SNNote extends DecryptedItem<NoteContent> implements NoteContentSpecialized {
public readonly title: string
public readonly text: string
public readonly mobilePrefersPlainEditor?: boolean
public readonly hidePreview: boolean = false
public readonly preview_plain: string
public readonly preview_html: string
public readonly prefersPlainEditor: boolean
public readonly spellcheck?: boolean
constructor(payload: DecryptedPayloadInterface<NoteContent>) {
super(payload)
this.title = String(this.payload.content.title || '')
this.text = String(this.payload.content.text || '')
this.preview_plain = String(this.payload.content.preview_plain || '')
this.preview_html = String(this.payload.content.preview_html || '')
this.hidePreview = Boolean(this.payload.content.hidePreview)
this.spellcheck = this.payload.content.spellcheck
this.prefersPlainEditor = this.getAppDomainValueWithDefault(AppDataField.PrefersPlainEditor, false)
this.mobilePrefersPlainEditor = this.payload.content.mobilePrefersPlainEditor
}
}

View File

@@ -0,0 +1,13 @@
import { ItemContent } from '../../Abstract/Content/ItemContent'
export interface NoteContentSpecialized {
title: string
text: string
mobilePrefersPlainEditor?: boolean
hidePreview?: boolean
preview_plain?: string
preview_html?: string
spellcheck?: boolean
}
export type NoteContent = NoteContentSpecialized & ItemContent

View File

@@ -0,0 +1,41 @@
import { AppDataField } from '../../Abstract/Item/Types/AppDataField'
import { NoteContent } from './NoteContent'
import { DecryptedItemMutator } from '../../Abstract/Item/Mutator/DecryptedItemMutator'
export class NoteMutator extends DecryptedItemMutator<NoteContent> {
set title(title: string) {
this.mutableContent.title = title
}
set text(text: string) {
this.mutableContent.text = text
}
set hidePreview(hidePreview: boolean) {
this.mutableContent.hidePreview = hidePreview
}
set preview_plain(preview_plain: string) {
this.mutableContent.preview_plain = preview_plain
}
set preview_html(preview_html: string | undefined) {
this.mutableContent.preview_html = preview_html
}
set prefersPlainEditor(prefersPlainEditor: boolean) {
this.setAppDataItem(AppDataField.PrefersPlainEditor, prefersPlainEditor)
}
set spellcheck(spellcheck: boolean) {
this.mutableContent.spellcheck = spellcheck
}
toggleSpellcheck(): void {
if (this.mutableContent.spellcheck == undefined) {
this.mutableContent.spellcheck = false
} else {
this.mutableContent.spellcheck = !this.mutableContent.spellcheck
}
}
}

View File

@@ -0,0 +1,3 @@
export * from './Note'
export * from './NoteMutator'
export * from './NoteContent'