-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(note): Merge user and note tools (#182)
* Merge user and note tools * Fix build * Remove line * Add note dto * Update src/application/services/useTools.ts Co-authored-by: Peter Savchenko <[email protected]> --------- Co-authored-by: Peter Savchenko <[email protected]>
- Loading branch information
1 parent
1d300e2
commit 0ca0e6a
Showing
10 changed files
with
146 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { type Ref, ref, watch } from 'vue'; | ||
import { useAppState } from './useAppState'; | ||
import type EditorTool from '@/domain/entities/EditorTool'; | ||
import { loadScript } from '@/infrastructure/utils/load-script'; | ||
import type { EditorConfig } from '@editorjs/editorjs'; | ||
|
||
/** | ||
* Downloaded tools data structure | ||
*/ | ||
type DownloadedTools = EditorConfig['tools']; | ||
|
||
/** | ||
* Service for load editor tools | ||
* | ||
* @param noteTools - note tools | ||
*/ | ||
export function useTools(noteTools: Ref<EditorTool[]>): { | ||
tools: Ref<DownloadedTools | undefined>; | ||
} { | ||
/** | ||
* User notes tools | ||
*/ | ||
const { userEditorTools } = useAppState(); | ||
const tools = ref<DownloadedTools | undefined>(); | ||
|
||
/** | ||
* Download all the user tools and return a map | ||
* | ||
* @param toolsList - tools data | ||
*/ | ||
async function downloadTools(toolsList: EditorTool[]): Promise<DownloadedTools> { | ||
const downloadedTools: DownloadedTools = {}; | ||
|
||
for (const tool of toolsList) { | ||
if (tool.source.cdn === undefined) { | ||
continue; | ||
} | ||
|
||
await loadScript(tool.source.cdn); | ||
|
||
downloadedTools[tool.name] = window[tool.exportName as keyof typeof window]; | ||
} | ||
|
||
return downloadedTools; | ||
} | ||
|
||
/** | ||
* Merge two arrays of tools, removing duplicates | ||
* | ||
* @param toolsA – first array of tools | ||
* @param toolsB – second array of tools | ||
*/ | ||
function mergeTools(toolsA: EditorTool[], toolsB: EditorTool[]): EditorTool[] { | ||
const uniqueTools = new Map<string, EditorTool>(); | ||
|
||
[...toolsA, ...toolsB].forEach((tool) => { | ||
uniqueTools.set(tool.name, tool); | ||
}); | ||
|
||
return Array.from(uniqueTools.values()); | ||
} | ||
|
||
watch( | ||
[userEditorTools, noteTools], | ||
async () => { | ||
const allTools = mergeTools(userEditorTools.value || [], noteTools.value); | ||
|
||
/** | ||
* If tools are not loaded yet or empty, skip downloading their scripts | ||
*/ | ||
if (allTools.length === 0) { | ||
return; | ||
} | ||
tools.value = await downloadTools(allTools); | ||
}, | ||
{ immediate: true } | ||
); | ||
|
||
return { | ||
tools, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type EditorTool from './EditorTool'; | ||
import type { Note } from './Note'; | ||
import type NoteAccessRights from './NoteAccessRights'; | ||
|
||
/** | ||
* Note Datsa Transfer Object used to pass Note data between layers | ||
*/ | ||
export interface NoteDTO { | ||
/** | ||
* Note data | ||
*/ | ||
note: Note; | ||
|
||
/** | ||
* Note access rights | ||
*/ | ||
accessRights: NoteAccessRights; | ||
|
||
/** | ||
* Parent note if exists | ||
*/ | ||
parentNote: Note | undefined; | ||
|
||
/** | ||
* Editor tools | ||
*/ | ||
tools: EditorTool[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Note } from './Note'; | ||
|
||
/** | ||
* On new note creation, we use predefined structure of the Editor: header + paragraph | ||
* We call it NoteDraft | ||
*/ | ||
export type NoteDraft = Pick<Note, 'content'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters