-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom the items of linked menu by configuration (#7554)
Close issue [BS-719](https://linear.app/affine-design/issue/BS-719) and [BS-791](https://linear.app/affine-design/issue/BS-791). Related PR in [BlockSuite](toeverything/blocksuite#7693). ### What Changed? - Support config in BlockSpec - AFFiNE’s Custom @menu - ignore docs in trash - ignore unedited journal - customized journal icon ![截屏2024-07-19 16.03.55.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/sJGviKxfE3Ap685cl5bj/cdf6f198-8288-4152-8893-65f17bc9983c.png) ### What's next? - getMenus returns an observable array - Add commands field to BlockSpec and encapsulated insertLinkedNode into a command
- Loading branch information
Showing
5 changed files
with
185 additions
and
33 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
119 changes: 119 additions & 0 deletions
119
.../frontend/core/src/components/blocksuite/block-suite-editor/specs/custom/linked-widget.ts
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,119 @@ | ||
import { WorkspacePropertiesAdapter } from '@affine/core/modules/properties'; | ||
import { mixpanel } from '@affine/core/utils'; | ||
import type { EditorHost } from '@blocksuite/block-std'; | ||
import type { AffineInlineEditor } from '@blocksuite/blocks'; | ||
import { LinkedWidgetUtils } from '@blocksuite/blocks'; | ||
import { | ||
LinkedEdgelessIcon, | ||
LinkedPageIcon, | ||
TodayIcon, | ||
} from '@blocksuite/icons/lit'; | ||
import type { DocMeta } from '@blocksuite/store'; | ||
import { | ||
DocsService, | ||
type FrameworkProvider, | ||
WorkspaceService, | ||
} from '@toeverything/infra'; | ||
|
||
export function createLinkedWidgetConfig(framework: FrameworkProvider) { | ||
return { | ||
getMenus: ( | ||
query: string, | ||
abort: () => void, | ||
editorHost: EditorHost, | ||
inlineEditor: AffineInlineEditor | ||
) => { | ||
const currentWorkspace = framework.get(WorkspaceService).workspace; | ||
const rawMetas = currentWorkspace.docCollection.meta.docMetas; | ||
const adapter = framework.get(WorkspacePropertiesAdapter); | ||
const isJournal = (d: DocMeta) => | ||
!!adapter.getJournalPageDateString(d.id); | ||
|
||
const docMetas = rawMetas | ||
.filter(meta => { | ||
if (isJournal(meta) && !meta.updatedDate) { | ||
return false; | ||
} | ||
return !meta.trash; | ||
}) | ||
.filter(({ title }) => isFuzzyMatch(title, query)); | ||
|
||
// TODO need i18n if BlockSuite supported | ||
const MAX_DOCS = 6; | ||
const DEFAULT_DOC_NAME = 'Untitled'; | ||
const docsService = framework.get(DocsService); | ||
const isEdgeless = (d: DocMeta) => | ||
docsService.list.getMode(d.id) === 'edgeless'; | ||
return Promise.resolve([ | ||
{ | ||
name: 'Link to Doc', | ||
items: docMetas.map(doc => ({ | ||
key: doc.id, | ||
name: doc.title || DEFAULT_DOC_NAME, | ||
icon: isJournal(doc) | ||
? TodayIcon | ||
: isEdgeless(doc) | ||
? LinkedEdgelessIcon | ||
: LinkedPageIcon, | ||
action: () => { | ||
abort(); | ||
LinkedWidgetUtils.insertLinkedNode({ | ||
inlineEditor, | ||
docId: doc.id, | ||
}); | ||
mixpanel.track('LinkedDocCreated', { | ||
control: 'linked doc', | ||
module: 'inline @', | ||
type: 'doc', | ||
other: 'existing doc', | ||
}); | ||
}, | ||
})), | ||
maxDisplay: MAX_DOCS, | ||
overflowText: `${docMetas.length - MAX_DOCS} more docs`, | ||
}, | ||
LinkedWidgetUtils.createNewDocMenuGroup( | ||
query, | ||
abort, | ||
editorHost, | ||
inlineEditor | ||
), | ||
]); | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Checks if the name is a fuzzy match of the query. | ||
* | ||
* @example | ||
* ```ts | ||
* const name = 'John Smith'; | ||
* const query = 'js'; | ||
* const isMatch = isFuzzyMatch(name, query); | ||
* // isMatch: true | ||
* ``` | ||
*/ | ||
function isFuzzyMatch(name: string, query: string) { | ||
const pureName = name | ||
.trim() | ||
.toLowerCase() | ||
.split('') | ||
.filter(char => char !== ' ') | ||
.join(''); | ||
|
||
const regex = new RegExp( | ||
query | ||
.split('') | ||
.filter(char => char !== ' ') | ||
.map(item => `${escapeRegExp(item)}.*`) | ||
.join(''), | ||
'i' | ||
); | ||
return regex.test(pureName); | ||
} | ||
|
||
function escapeRegExp(input: string) { | ||
// escape regex characters in the input string to prevent regex format errors | ||
return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
} |
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