2022-04-19 09:38:31 +00:00
|
|
|
import { Doc } from '@anticrm/core'
|
|
|
|
import { onDestroy } from 'svelte'
|
|
|
|
import { writable } from 'svelte/store'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export type SelectDirection = 'vertical' | 'horizontal'
|
|
|
|
|
|
|
|
export interface SelectionFocusProvider {
|
2022-04-24 05:18:03 +00:00
|
|
|
// -1 - previous
|
|
|
|
// 0 - selec of as current
|
|
|
|
// 1 - next
|
2022-04-19 09:38:31 +00:00
|
|
|
// * If vertical, next will return item under.
|
|
|
|
// * If horizontal, next will return item on right.
|
2022-04-24 05:18:03 +00:00
|
|
|
// of - document offset from we requesting.
|
|
|
|
select?: (offset: 1 | -1 | 0, of?: Doc, direction?: SelectDirection) => void
|
2022-04-19 09:38:31 +00:00
|
|
|
|
|
|
|
// Update documents content
|
|
|
|
update: (docs: Doc[]) => void
|
|
|
|
|
|
|
|
// Return selection index from list of documents.
|
|
|
|
current: (doc?: FocusSelection) => number | undefined
|
|
|
|
|
|
|
|
// Update focused element, selection is not changed.
|
|
|
|
updateFocus: (doc: Doc) => void
|
|
|
|
|
|
|
|
// Update curent selection list, focus items it not updated.
|
|
|
|
updateSelection: (docs: Doc[], value: boolean) => void
|
|
|
|
|
|
|
|
// Return all selectable documents
|
|
|
|
docs: () => Doc[]
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
* Define document selection inside platform.
|
|
|
|
*/
|
|
|
|
export interface FocusSelection {
|
|
|
|
// Focused document
|
|
|
|
focus?: Doc
|
|
|
|
|
|
|
|
// Additional interface to select, next/prev etc.
|
|
|
|
provider?: SelectionFocusProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-04-29 05:27:17 +00:00
|
|
|
export const focusStore = writable<FocusSelection>({})
|
2022-04-19 09:38:31 +00:00
|
|
|
export const selectionStore = writable<Doc[]>([])
|
|
|
|
|
2022-04-29 05:27:17 +00:00
|
|
|
export const previewDocument = writable<Doc | undefined>()
|
2022-04-19 09:38:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function updateFocus (selection?: FocusSelection): void {
|
|
|
|
focusStore.update((cur) => {
|
|
|
|
const now = Date.now()
|
|
|
|
if (selection === undefined || now - ((cur as any).now ?? 0) >= 25) {
|
|
|
|
cur.focus = selection?.focus
|
|
|
|
cur.provider = selection?.provider
|
|
|
|
;(cur as any).now = now
|
2022-04-29 05:27:17 +00:00
|
|
|
previewDocument.update((old) => {
|
2022-04-19 09:38:31 +00:00
|
|
|
if (old !== undefined) {
|
|
|
|
return selection?.focus
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return cur
|
|
|
|
})
|
|
|
|
|
|
|
|
// We need to clear selection items not belong to passed provider.
|
|
|
|
if (selection?.provider !== undefined) {
|
2022-04-29 05:27:17 +00:00
|
|
|
const docs = new Set(selection?.provider.docs().map((it) => it._id))
|
2022-04-19 09:38:31 +00:00
|
|
|
selectionStore.update((old) => {
|
2022-04-29 05:27:17 +00:00
|
|
|
return old.filter((it) => docs.has(it._id))
|
2022-04-19 09:38:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*
|
|
|
|
* List selection provider
|
|
|
|
*/
|
|
|
|
export class ListSelectionProvider implements SelectionFocusProvider {
|
|
|
|
_docs: Doc[] = []
|
|
|
|
_current?: FocusSelection
|
2022-04-29 05:27:17 +00:00
|
|
|
constructor (private readonly delegate: (offset: 1 | -1 | 0, of?: Doc, direction?: SelectDirection) => void) {
|
2022-04-19 09:38:31 +00:00
|
|
|
const unsubscribe = focusStore.subscribe((doc) => {
|
|
|
|
this._current = doc
|
|
|
|
})
|
|
|
|
onDestroy(() => {
|
|
|
|
unsubscribe()
|
|
|
|
updateFocus(undefined)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-24 05:18:03 +00:00
|
|
|
select (offset: 1 | -1 | 0, of?: Doc, direction?: SelectDirection): void {
|
|
|
|
this.delegate(offset, of, direction)
|
2022-04-19 09:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update (docs: Doc[]): void {
|
|
|
|
this._docs = docs
|
|
|
|
|
|
|
|
if (this._docs.length > 0) {
|
2022-04-24 05:18:03 +00:00
|
|
|
if (this._current?.focus === undefined) {
|
|
|
|
this.delegate(0, undefined, 'vertical')
|
2022-04-19 09:38:31 +00:00
|
|
|
} else {
|
|
|
|
// Check if we don't have object, we need to select first one.
|
2022-04-24 05:18:03 +00:00
|
|
|
this.delegate(0, this._current?.focus, 'vertical')
|
2022-04-19 09:38:31 +00:00
|
|
|
}
|
2022-04-24 05:18:03 +00:00
|
|
|
updateFocus({ focus: this._current?.focus, provider: this })
|
2022-04-19 09:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
docs (): Doc[] {
|
|
|
|
return this._docs
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFocus (doc: Doc): void {
|
|
|
|
updateFocus({ focus: doc, provider: this })
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSelection (docs: Doc[], value: boolean): void {
|
|
|
|
selectionStore.update((selection) => {
|
2022-04-29 05:27:17 +00:00
|
|
|
const docsSet = new Set(docs.map((it) => it._id))
|
2022-04-19 09:38:31 +00:00
|
|
|
const noDocs = selection.filter((it) => !docsSet.has(it._id))
|
2022-04-29 05:27:17 +00:00
|
|
|
return value ? [...noDocs, ...docs] : noDocs
|
2022-04-19 09:38:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
current (doc?: FocusSelection): number | undefined {
|
|
|
|
return this._docs.findIndex((it) => it._id === doc?.focus?._id)
|
|
|
|
}
|
|
|
|
}
|