refactor: lexical (#1954)

This commit is contained in:
Mo
2022-11-07 10:42:44 -06:00
committed by GitHub
parent 99bae83f8b
commit 2ed01a071c
182 changed files with 8525 additions and 1126 deletions

View File

@@ -1,4 +1,3 @@
import { NoteType } from '@standardnotes/features'
import { createNote } from './../../Utilities/Test/SpecUtils'
describe('SNNote Tests', () => {
@@ -35,61 +34,4 @@ describe('SNNote Tests', () => {
expect(note.noteType).toBe(undefined)
})
it('should getBlock', () => {
const note = createNote({
text: 'some text',
blocksItem: {
blocks: [
{
id: '123',
type: NoteType.Authentication,
editorIdentifier: '456',
content: 'foo',
},
],
},
})
expect(note.getBlock('123')).toStrictEqual({
id: '123',
type: NoteType.Authentication,
editorIdentifier: '456',
content: 'foo',
})
})
it('should getBlock with no blocks', () => {
const note = createNote({
text: 'some text',
})
expect(note.getBlock('123')).toBe(undefined)
})
it('should getBlock with no blocksItem', () => {
const note = createNote({
text: 'some text',
})
expect(note.getBlock('123')).toBe(undefined)
})
it('should get indexOfBlock', () => {
const note = createNote({
text: 'some text',
blocksItem: {
blocks: [
{
id: '123',
type: NoteType.Authentication,
editorIdentifier: '456',
content: 'foo',
},
],
},
})
expect(note.indexOfBlock({ id: '123' })).toBe(0)
})
})

View File

@@ -5,7 +5,6 @@ import { DecryptedItem } from '../../Abstract/Item/Implementations/DecryptedItem
import { ItemInterface } from '../../Abstract/Item/Interfaces/ItemInterface'
import { DecryptedPayloadInterface } from '../../Abstract/Payload/Interfaces/DecryptedPayload'
import { NoteContent, NoteContentSpecialized } from './NoteContent'
import { NoteBlock, NoteBlocks } from './NoteBlocks'
export const isNote = (x: ItemInterface): x is SNNote => x.content_type === ContentType.Note
@@ -18,7 +17,6 @@ export class SNNote extends DecryptedItem<NoteContent> implements NoteContentSpe
public readonly spellcheck?: boolean
public readonly noteType?: NoteType
public readonly authorizedForListed: boolean
public readonly blocksItem?: NoteBlocks
/** The package_info.identifier of the editor (not its uuid), such as org.standardnotes.advanced-markdown */
public readonly editorIdentifier?: FeatureIdentifier | string
@@ -28,14 +26,13 @@ export class SNNote extends DecryptedItem<NoteContent> implements NoteContentSpe
this.title = String(this.payload.content.title || '')
this.text = String(this.payload.content.text || '')
this.hidePreview = Boolean(this.payload.content.hidePreview)
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.noteType = this.payload.content.noteType
this.editorIdentifier = this.payload.content.editorIdentifier
this.authorizedForListed = this.payload.content.authorizedForListed || false
this.blocksItem = this.payload.content.blocksItem
if (!this.noteType) {
const prefersPlain = this.getAppDomainValueWithDefault(AppDataField.LegacyPrefersPlainEditor, false)
@@ -44,21 +41,4 @@ export class SNNote extends DecryptedItem<NoteContent> implements NoteContentSpe
}
}
}
getBlock(id: string): NoteBlock | undefined {
return this.blocksItem?.blocks.find((block) => block.id === id)
}
indexOfBlock(block: { id: string }): number | undefined {
if (!this.blocksItem) {
return undefined
}
const index = this.blocksItem.blocks.findIndex((b) => b.id === block.id)
if (index === -1) {
return undefined
}
return index
}
}

View File

@@ -1,13 +0,0 @@
import { NoteType } from '@standardnotes/features'
export type NoteBlock = {
id: string
type: NoteType
editorIdentifier: string
content: string
size?: { width: number; height: number }
}
export interface NoteBlocks {
blocks: NoteBlock[]
}

View File

@@ -1,6 +1,5 @@
import { FeatureIdentifier, NoteType } from '@standardnotes/features'
import { ItemContent } from '../../Abstract/Content/ItemContent'
import { NoteBlocks } from './NoteBlocks'
export interface NoteContentSpecialized {
title: string
@@ -12,7 +11,6 @@ export interface NoteContentSpecialized {
noteType?: NoteType
editorIdentifier?: FeatureIdentifier | string
authorizedForListed?: boolean
blocksItem?: NoteBlocks
}
export type NoteContent = NoteContentSpecialized & ItemContent

View File

@@ -13,7 +13,7 @@ describe('note mutator', () => {
expect(result.content.noteType).toEqual(NoteType.Authentication)
})
it('sets editorIdentifier', () => {
it('sets componentIdentifier', () => {
const note = createNote({})
const mutator = new NoteMutator(note, MutationType.NoUpdateUserTimestamps)
mutator.editorIdentifier = FeatureIdentifier.MarkdownProEditor
@@ -21,99 +21,4 @@ describe('note mutator', () => {
expect(result.content.editorIdentifier).toEqual(FeatureIdentifier.MarkdownProEditor)
})
it('should addBlock to new note', () => {
const note = createNote({})
const mutator = new NoteMutator(note, MutationType.NoUpdateUserTimestamps)
mutator.addBlock({ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'test' })
const result = mutator.getResult()
expect(result.content.blocksItem).toEqual({
blocks: [{ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'test' }],
})
})
it('should addBlock to existing note', () => {
const note = createNote({
blocksItem: {
blocks: [{ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'test' }],
},
})
const mutator = new NoteMutator(note, MutationType.NoUpdateUserTimestamps)
mutator.addBlock({ type: NoteType.RichText, id: '456', editorIdentifier: 'richy', content: 'test' })
const result = mutator.getResult()
expect(result.content.blocksItem).toEqual({
blocks: [
{ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'test' },
{ type: NoteType.RichText, id: '456', editorIdentifier: 'richy', content: 'test' },
],
})
})
it('should removeBlock', () => {
const note = createNote({
blocksItem: {
blocks: [
{ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'test' },
{ type: NoteType.Code, id: '456', editorIdentifier: 'markdown', content: 'test' },
],
},
})
const mutator = new NoteMutator(note, MutationType.NoUpdateUserTimestamps)
mutator.removeBlock({ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'test' })
const result = mutator.getResult()
expect(result.content.blocksItem).toEqual({
blocks: [{ type: NoteType.Code, id: '456', editorIdentifier: 'markdown', content: 'test' }],
})
})
it('should changeBlockContent', () => {
const note = createNote({
blocksItem: {
blocks: [
{ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'old content 1' },
{ type: NoteType.Code, id: '456', editorIdentifier: 'markdown', content: 'old content 2' },
],
},
})
const mutator = new NoteMutator(note, MutationType.NoUpdateUserTimestamps)
mutator.changeBlockContent('123', 'new content')
const result = mutator.getResult()
expect(result.content.blocksItem).toEqual({
blocks: [
{ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'new content' },
{ type: NoteType.Code, id: '456', editorIdentifier: 'markdown', content: 'old content 2' },
],
})
})
it('should changeBlockSize', () => {
const note = createNote({
blocksItem: {
blocks: [
{ type: NoteType.Code, id: '123', editorIdentifier: 'markdown', content: 'test' },
{ type: NoteType.Code, id: '456', editorIdentifier: 'markdown', content: 'test' },
],
},
})
const mutator = new NoteMutator(note, MutationType.NoUpdateUserTimestamps)
mutator.changeBlockSize('123', { width: 10, height: 20 })
const result = mutator.getResult()
expect(result.content.blocksItem).toEqual({
blocks: [
{
type: NoteType.Code,
id: '123',
editorIdentifier: 'markdown',
content: 'test',
size: { width: 10, height: 20 },
},
{ type: NoteType.Code, id: '456', editorIdentifier: 'markdown', content: 'test' },
],
})
})
})

View File

@@ -5,8 +5,6 @@ import { NoteToNoteReference } from '../../Abstract/Reference/NoteToNoteReferenc
import { ContentType } from '@standardnotes/common'
import { ContentReferenceType } from '../../Abstract/Item'
import { FeatureIdentifier, NoteType } from '@standardnotes/features'
import { NoteBlock } from './NoteBlocks'
import { filterFromArray } from '@standardnotes/utils'
export class NoteMutator extends DecryptedItemMutator<NoteContent> {
set title(title: string) {
@@ -45,48 +43,6 @@ export class NoteMutator extends DecryptedItemMutator<NoteContent> {
this.mutableContent.authorizedForListed = authorizedForListed
}
addBlock(block: NoteBlock): void {
if (!this.mutableContent.blocksItem) {
this.mutableContent.blocksItem = { blocks: [] }
}
this.mutableContent.blocksItem.blocks.push(block)
}
removeBlock(block: NoteBlock): void {
if (!this.mutableContent.blocksItem) {
return
}
filterFromArray(this.mutableContent.blocksItem.blocks, { id: block.id })
}
changeBlockContent(blockId: string, content: string): void {
const blockIndex = this.mutableContent.blocksItem?.blocks.findIndex((b) => {
return b.id === blockId
})
if (blockIndex == null || blockIndex === -1) {
return
}
const block = this.mutableContent.blocksItem?.blocks[blockIndex]
if (!block) {
return
}
block.content = content
}
changeBlockSize(blockId: string, size: { width: number; height: number }): void {
const block = this.mutableContent.blocksItem?.blocks.find((b) => b.id === blockId)
if (!block) {
return
}
block.size = size
}
toggleSpellcheck(): void {
if (this.mutableContent.spellcheck == undefined) {
this.mutableContent.spellcheck = false

View File

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