@@ -13,7 +13,6 @@ import {
1313 assertUniqueSiblingEntryName ,
1414 createSnippetRecord ,
1515 findFolderById ,
16- findSnippetByContentId ,
1716 findSnippetById ,
1817 getPaths ,
1918 getRuntimeCache ,
@@ -28,11 +27,7 @@ import {
2827 validateEntryName ,
2928 writeSnippetToFile ,
3029} from '../runtime'
31- import {
32- createNestedContent ,
33- deleteNestedContent ,
34- updateNestedContent ,
35- } from '../runtime/shared/entityContent'
30+ import { createNestedContent } from '../runtime/shared/entityContent'
3631import { filterAndSortByQuery } from '../runtime/shared/entityQuery'
3732import {
3833 addTagToEntity ,
@@ -44,6 +39,13 @@ import {
4439 getEntityDeleteCounts ,
4540} from '../runtime/shared/entityStorage'
4641
42+ function findContentIndexById (
43+ snippet : MarkdownSnippet ,
44+ contentId : number ,
45+ ) : number {
46+ return snippet . contents . findIndex ( content => content . id === contentId )
47+ }
48+
4749export function createSnippetsStorage ( ) : SnippetsStorage {
4850 return {
4951 getSnippets : ( query : SnippetsQueryInput ) => {
@@ -229,39 +231,56 @@ export function createSnippetsStorage(): SnippetsStorage {
229231 ) : SnippetContentUpdateResult => {
230232 const paths = getPaths ( getVaultPath ( ) )
231233 const { state, snippets } = getRuntimeCache ( paths )
232- const ownedContent = findSnippetByContentId ( snippets , contentId )
233- const result = updateNestedContent ( {
234- applyPatch : ( content , patch ) => {
235- if ( 'label' in patch ) {
236- content . label = patch . label || content . label
237- }
238234
239- if ( 'value' in patch ) {
240- content . value = patch . value ?? null
241- }
235+ if ( ! ( 'label' in input || 'value' in input || 'language' in input ) ) {
236+ return {
237+ invalidInput : true ,
238+ notFound : false ,
239+ parentNotFound : false ,
240+ }
241+ }
242242
243- if ( 'language' in patch ) {
244- content . language = patch . language || content . language
245- }
246- } ,
247- findTargetOwnerById : id => findSnippetById ( snippets , id ) ,
248- hasAnyField : patch =>
249- 'label' in patch || 'value' in patch || 'language' in patch ,
250- ownerId : snippetId ,
251- ownedContent : ownedContent
252- ? {
253- contentIndex : ownedContent . contentIndex ,
254- owner : ownedContent . snippet ,
255- }
256- : undefined ,
257- patch : input ,
258- persistOwner : snippet => writeSnippetToFile ( paths , snippet ) ,
259- } )
260- if ( ! result . invalidInput && ! result . notFound ) {
261- saveState ( paths , state )
243+ const snippet = findSnippetById ( snippets , snippetId )
244+ if ( ! snippet ) {
245+ return {
246+ invalidInput : false ,
247+ notFound : false ,
248+ parentNotFound : true ,
249+ }
262250 }
263251
264- return result
252+ const contentIndex = findContentIndexById ( snippet , contentId )
253+ if ( contentIndex === - 1 ) {
254+ return {
255+ invalidInput : false ,
256+ notFound : true ,
257+ parentNotFound : false ,
258+ }
259+ }
260+
261+ const content = snippet . contents [ contentIndex ]
262+
263+ if ( 'label' in input ) {
264+ content . label = input . label || content . label
265+ }
266+
267+ if ( 'value' in input ) {
268+ content . value = input . value ?? null
269+ }
270+
271+ if ( 'language' in input ) {
272+ content . language = input . language || content . language
273+ }
274+
275+ snippet . updatedAt = Date . now ( )
276+ writeSnippetToFile ( paths , snippet )
277+ saveState ( paths , state )
278+
279+ return {
280+ invalidInput : false ,
281+ notFound : false ,
282+ parentNotFound : false ,
283+ }
265284 } ,
266285 addTagToSnippet : ( snippetId , tagId ) : SnippetTagRelationResult => {
267286 const paths = getPaths ( getVaultPath ( ) )
@@ -345,26 +364,26 @@ export function createSnippetsStorage(): SnippetsStorage {
345364
346365 return result
347366 } ,
348- deleteSnippetContent : ( contentId ) => {
367+ deleteSnippetContent : ( snippetId , contentId ) => {
349368 const paths = getPaths ( getVaultPath ( ) )
350369 const { state, snippets } = getRuntimeCache ( paths )
351- const ownedContent = findSnippetByContentId ( snippets , contentId )
352- const result = deleteNestedContent ( {
353- ownedContent : ownedContent
354- ? {
355- contentIndex : ownedContent . contentIndex ,
356- owner : ownedContent . snippet ,
357- }
358- : undefined ,
359- persistOwner : snippet => writeSnippetToFile ( paths , snippet ) ,
360- } )
361- if ( ! result . deleted ) {
362- return result
370+ const snippet = findSnippetById ( snippets , snippetId )
371+
372+ if ( ! snippet ) {
373+ return { deleted : false }
374+ }
375+
376+ const contentIndex = findContentIndexById ( snippet , contentId )
377+ if ( contentIndex === - 1 ) {
378+ return { deleted : false }
363379 }
364380
381+ snippet . contents . splice ( contentIndex , 1 )
382+ snippet . updatedAt = Date . now ( )
383+ writeSnippetToFile ( paths , snippet )
365384 saveState ( paths , state )
366385
367- return result
386+ return { deleted : true }
368387 } ,
369388 }
370389}
0 commit comments