refactor: de-couple linking controller from active item (#2108)

This commit is contained in:
Aman Harwara
2022-12-19 20:08:57 +05:30
committed by GitHub
parent 2b84c242f7
commit 31bb03943d
9 changed files with 211 additions and 200 deletions

View File

@@ -245,8 +245,6 @@ export class ItemListController extends AbstractViewController implements Intern
await this.application.itemControllerGroup.createItemController({ note })
this.linkingController.reloadAllLinks()
await this.publishCrossControllerEventSync(CrossControllerEvent.ActiveEditorChanged)
}
@@ -262,8 +260,6 @@ export class ItemListController extends AbstractViewController implements Intern
}
await this.application.itemControllerGroup.createItemController({ file })
this.linkingController.reloadAllLinks()
}
setCompletedFullSync = (completed: boolean) => {
@@ -659,8 +655,6 @@ export class ItemListController extends AbstractViewController implements Intern
const controller = await this.createNewNoteController(useTitle, createdAt, autofocusBehavior)
this.linkingController.reloadAllLinks()
this.selectionController.scrollToItem(controller.item)
}

View File

@@ -4,7 +4,6 @@ import { NoteViewController } from '@/Components/NoteView/Controller/NoteViewCon
import { AppPaneId } from '@/Components/Panes/AppPaneMetadata'
import { PrefDefaults } from '@/Constants/PrefDefaults'
import { createLinkFromItem } from '@/Utils/Items/Search/createLinkFromItem'
import { ItemLink } from '@/Utils/Items/Search/ItemLink'
import { LinkableItem } from '@/Utils/Items/Search/LinkableItem'
import {
ApplicationEvent,
@@ -28,11 +27,6 @@ import { SelectedItemsController } from './SelectedItemsController'
import { SubscriptionController } from './Subscription/SubscriptionController'
export class LinkingController extends AbstractViewController {
tags: ItemLink<SNTag>[] = []
linkedFiles: ItemLink<FileItem>[] = []
filesLinkingToActiveItem: ItemLink<FileItem>[] = []
notesLinkedToItem: ItemLink<SNNote>[] = []
notesLinkingToActiveItem: ItemLink<SNNote>[] = []
shouldLinkToParentFolders: boolean
isLinkingPanelOpen = false
private itemListController!: ItemListController
@@ -48,22 +42,11 @@ export class LinkingController extends AbstractViewController {
super(application, eventBus)
makeObservable(this, {
tags: observable,
linkedFiles: observable,
filesLinkingToActiveItem: observable,
notesLinkedToItem: observable,
notesLinkingToActiveItem: observable,
isLinkingPanelOpen: observable,
allItemLinks: computed,
isEntitledToNoteLinking: computed,
selectedItemTitle: computed,
setIsLinkingPanelOpen: action,
reloadLinkedFiles: action,
reloadLinkedTags: action,
reloadLinkedNotes: action,
reloadNotesLinkingToItem: action,
})
this.shouldLinkToParentFolders = application.getPreference(
@@ -89,19 +72,6 @@ export class LinkingController extends AbstractViewController {
this.itemListController = itemListController
this.filesController = filesController
this.subscriptionController = subscriptionController
this.disposers.push(
this.application.streamItems(ContentType.File, () => {
this.reloadLinkedFiles()
}),
this.application.streamItems(ContentType.Tag, () => {
this.reloadLinkedTags()
}),
this.application.streamItems(ContentType.Note, () => {
this.reloadLinkedNotes()
this.reloadNotesLinkingToItem()
}),
)
}
get isEntitledToNoteLinking() {
@@ -112,87 +82,61 @@ export class LinkingController extends AbstractViewController {
this.isLinkingPanelOpen = open
}
get allItemLinks() {
return [...this.tags, ...this.linkedFiles, ...this.notesLinkedToItem]
}
get activeItem() {
return this.application.itemControllerGroup.activeItemViewController?.item
}
get selectedItemTitle() {
return this.selectionController.firstSelectedItem
? this.selectionController.firstSelectedItem.title
: this.activeItem
? this.activeItem.title
: ''
}
reloadAllLinks() {
this.reloadLinkedFiles()
this.reloadLinkedTags()
this.reloadLinkedNotes()
this.reloadNotesLinkingToItem()
}
reloadLinkedFiles() {
if (!this.activeItem || this.application.items.isTemplateItem(this.activeItem)) {
this.linkedFiles = []
this.filesLinkingToActiveItem = []
return
getFilesLinksForItem = (item: LinkableItem | undefined) => {
if (!item || this.application.items.isTemplateItem(item)) {
return {
filesLinkedToItem: [],
filesLinkingToItem: [],
}
}
const referencesOfActiveItem = naturalSort(
this.application.items.referencesForItem(this.activeItem).filter(isFile),
'title',
)
const referencesOfItem = naturalSort(this.application.items.referencesForItem(item).filter(isFile), 'title')
const referencingActiveItem = naturalSort(
this.application.items.itemsReferencingItem(this.activeItem).filter(isFile),
'title',
)
const referencingItem = naturalSort(this.application.items.itemsReferencingItem(item).filter(isFile), 'title')
if (this.activeItem.content_type === ContentType.File) {
this.linkedFiles = referencesOfActiveItem.map((item) => createLinkFromItem(item, 'linked'))
this.filesLinkingToActiveItem = referencingActiveItem.map((item) => createLinkFromItem(item, 'linked-by'))
if (item.content_type === ContentType.File) {
return {
filesLinkedToItem: referencesOfItem.map((item) => createLinkFromItem(item, 'linked')),
filesLinkingToItem: referencingItem.map((item) => createLinkFromItem(item, 'linked-by')),
}
} else {
this.linkedFiles = referencingActiveItem.map((item) => createLinkFromItem(item, 'linked'))
this.filesLinkingToActiveItem = referencesOfActiveItem.map((item) => createLinkFromItem(item, 'linked-by'))
return {
filesLinkedToItem: referencingItem.map((item) => createLinkFromItem(item, 'linked')),
filesLinkingToItem: referencesOfItem.map((item) => createLinkFromItem(item, 'linked-by')),
}
}
}
reloadLinkedTags() {
if (!this.activeItem) {
getLinkedTagsForItem = (item: LinkableItem | undefined) => {
if (!item) {
return
}
this.tags = this.application.items
.getSortedTagsForItem(this.activeItem)
.map((item) => createLinkFromItem(item, 'linked'))
return this.application.items.getSortedTagsForItem(item).map((tag) => createLinkFromItem(tag, 'linked'))
}
reloadLinkedNotes() {
if (!this.activeItem || this.application.items.isTemplateItem(this.activeItem)) {
this.notesLinkedToItem = []
return
getLinkedNotesForItem = (item: LinkableItem | undefined) => {
if (!item || this.application.items.isTemplateItem(item)) {
return []
}
this.notesLinkedToItem = naturalSort(
this.application.items.referencesForItem(this.activeItem).filter(isNote),
'title',
).map((item) => createLinkFromItem(item, 'linked'))
return naturalSort(this.application.items.referencesForItem(item).filter(isNote), 'title').map((item) =>
createLinkFromItem(item, 'linked'),
)
}
reloadNotesLinkingToItem() {
if (!this.activeItem) {
this.notesLinkingToActiveItem = []
return
getNotesLinkingToItem = (item: LinkableItem | undefined) => {
if (!item) {
return []
}
this.notesLinkingToActiveItem = naturalSort(
this.application.items.itemsReferencingItem(this.activeItem).filter(isNote),
'title',
).map((item) => createLinkFromItem(item, 'linked-by'))
return naturalSort(this.application.items.itemsReferencingItem(item).filter(isNote), 'title').map((item) =>
createLinkFromItem(item, 'linked-by'),
)
}
activateItem = async (item: LinkableItem): Promise<AppPaneId | undefined> => {
@@ -230,7 +174,6 @@ export class LinkingController extends AbstractViewController {
await this.application.items.unlinkItems(selectedItem, itemToUnlink)
void this.application.sync.sync()
this.reloadAllLinks()
}
ensureActiveItemIsInserted = async () => {
@@ -240,11 +183,22 @@ export class LinkingController extends AbstractViewController {
}
}
linkItems = async (item: SNNote | FileItem, itemToLink: LinkableItem) => {
linkItems = async (item: LinkableItem, itemToLink: LinkableItem) => {
if (item instanceof SNNote) {
if (itemToLink instanceof SNNote && !this.isEntitledToNoteLinking) {
void this.publishCrossControllerEventSync(CrossControllerEvent.DisplayPremiumModal, {
featureName: 'Note linking',
})
return
}
if (item.uuid === this.activeItem?.uuid) {
await this.ensureActiveItemIsInserted()
}
if (itemToLink instanceof FileItem) {
await this.application.items.associateFileWithNote(itemToLink, item)
} else if (itemToLink instanceof SNNote && this.isEntitledToNoteLinking) {
} else if (itemToLink instanceof SNNote) {
await this.application.items.linkNoteToNote(item, itemToLink)
} else if (itemToLink instanceof SNTag) {
await this.addTagToItem(itemToLink, item)
@@ -266,7 +220,6 @@ export class LinkingController extends AbstractViewController {
}
void this.application.sync.sync()
this.reloadAllLinks()
}
linkItemToSelectedItem = async (itemToLink: LinkableItem): Promise<boolean> => {
@@ -307,7 +260,6 @@ export class LinkingController extends AbstractViewController {
await this.application.items.addTagToFile(item, tag, this.shouldLinkToParentFolders)
}
this.reloadLinkedTags()
this.application.sync.sync().catch(console.error)
}
}