refactor: extract components to plugin repo (#1933)
This commit is contained in:
@@ -5,6 +5,7 @@ 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
|
||||
|
||||
@@ -17,6 +18,7 @@ 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
|
||||
@@ -33,6 +35,7 @@ export class SNNote extends DecryptedItem<NoteContent> implements NoteContentSpe
|
||||
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)
|
||||
@@ -41,4 +44,8 @@ export class SNNote extends DecryptedItem<NoteContent> implements NoteContentSpe
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getBlock(id: string): NoteBlock | undefined {
|
||||
return this.blocksItem?.blocks.find((block) => block.id === id)
|
||||
}
|
||||
}
|
||||
|
||||
44
packages/models/src/Domain/Syncable/Note/NoteBlocks.ts
Normal file
44
packages/models/src/Domain/Syncable/Note/NoteBlocks.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { NoteType } from '@standardnotes/features'
|
||||
|
||||
export type NoteBlock = {
|
||||
id: string
|
||||
type: NoteType
|
||||
editorIdentifier: string
|
||||
size?: { width: number; height: number }
|
||||
content: string
|
||||
}
|
||||
|
||||
export interface NoteBlocks {
|
||||
blocks: NoteBlock[]
|
||||
}
|
||||
|
||||
export function bracketSyntaxForBlock(block: { id: NoteBlock['id'] }): { open: string; close: string } {
|
||||
return {
|
||||
open: `<Block id=${block.id}>`,
|
||||
close: `</Block id=${block.id}>`,
|
||||
}
|
||||
}
|
||||
|
||||
export function stringIndexOfBlock(
|
||||
text: string,
|
||||
block: { id: NoteBlock['id'] },
|
||||
): { begin: number; end: number } | undefined {
|
||||
const brackets = bracketSyntaxForBlock(block)
|
||||
|
||||
const startIndex = text.indexOf(brackets.open)
|
||||
if (startIndex === -1) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const endIndex = text.indexOf(brackets.close) + brackets.close.length
|
||||
|
||||
return {
|
||||
begin: startIndex,
|
||||
end: endIndex,
|
||||
}
|
||||
}
|
||||
|
||||
export function blockContentToNoteTextRendition(block: { id: NoteBlock['id'] }, content: string): string {
|
||||
const brackets = bracketSyntaxForBlock(block)
|
||||
return `${brackets.open}${content}${brackets.close}`
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { FeatureIdentifier, NoteType } from '@standardnotes/features'
|
||||
import { ItemContent } from '../../Abstract/Content/ItemContent'
|
||||
import { NoteBlocks } from './NoteBlocks'
|
||||
|
||||
export interface NoteContentSpecialized {
|
||||
title: string
|
||||
@@ -11,6 +12,7 @@ export interface NoteContentSpecialized {
|
||||
noteType?: NoteType
|
||||
editorIdentifier?: FeatureIdentifier | string
|
||||
authorizedForListed?: boolean
|
||||
blocksItem?: NoteBlocks
|
||||
}
|
||||
|
||||
export type NoteContent = NoteContentSpecialized & ItemContent
|
||||
|
||||
@@ -5,6 +5,8 @@ import { NoteToNoteReference } from '../../Abstract/Reference/NoteToNoteReferenc
|
||||
import { ContentType } from '@standardnotes/common'
|
||||
import { ContentReferenceType } from '../../Abstract/Item'
|
||||
import { FeatureIdentifier, NoteType } from '@standardnotes/features'
|
||||
import { blockContentToNoteTextRendition, bracketSyntaxForBlock, NoteBlock, stringIndexOfBlock } from './NoteBlocks'
|
||||
import { removeFromArray } from '@standardnotes/utils'
|
||||
|
||||
export class NoteMutator extends DecryptedItemMutator<NoteContent> {
|
||||
set title(title: string) {
|
||||
@@ -43,6 +45,65 @@ 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)
|
||||
|
||||
const brackets = bracketSyntaxForBlock(block)
|
||||
|
||||
this.text += `${brackets.open}${block.content}${brackets.close}`
|
||||
}
|
||||
|
||||
removeBlock(block: NoteBlock): void {
|
||||
if (!this.mutableContent.blocksItem) {
|
||||
return
|
||||
}
|
||||
|
||||
removeFromArray(this.mutableContent.blocksItem.blocks, block)
|
||||
|
||||
const location = stringIndexOfBlock(this.mutableContent.text, block)
|
||||
|
||||
if (location) {
|
||||
this.mutableContent.text = this.mutableContent.text.slice(location.begin, location.end)
|
||||
}
|
||||
}
|
||||
|
||||
changeBlockContent(blockId: string, content: string): void {
|
||||
const block = this.mutableContent.blocksItem?.blocks.find((b) => b.id === blockId)
|
||||
if (!block) {
|
||||
return
|
||||
}
|
||||
|
||||
block.content = content
|
||||
|
||||
const location = stringIndexOfBlock(this.mutableContent.text, block)
|
||||
|
||||
if (location) {
|
||||
const replaceRange = (s: string, start: number, end: number, substitute: string) => {
|
||||
return s.substring(0, start) + substitute + s.substring(end)
|
||||
}
|
||||
|
||||
this.mutableContent.text = replaceRange(
|
||||
this.mutableContent.text,
|
||||
location.begin,
|
||||
location.end,
|
||||
blockContentToNoteTextRendition({ id: blockId }, 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
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './Note'
|
||||
export * from './NoteMutator'
|
||||
export * from './NoteContent'
|
||||
export * from './NoteBlocks'
|
||||
|
||||
Reference in New Issue
Block a user