Ok working

This commit is contained in:
Eric Bailey
2024-09-09 22:09:37 -05:00
parent 7ae1321608
commit 927584e914
3 changed files with 27 additions and 17 deletions
@@ -94,7 +94,9 @@ export const TagsPluginKey = new PluginKey('tags')
* This extension allows you to insert mentions into the editor. * This extension allows you to insert mentions into the editor.
* @see https://www.tiptap.dev/api/extensions/mention * @see https://www.tiptap.dev/api/extensions/mention
*/ */
export const Tags = Node.create<MentionOptions>({ export const Tags = Node.create<
MentionOptions<any, {tag: string; punctuation?: string}>
>({
name: 'tags', name: 'tags',
addOptions() { addOptions() {
@@ -115,6 +117,8 @@ export const Tags = Node.create<MentionOptions>({
char: '#', char: '#',
pluginKey: TagsPluginKey, pluginKey: TagsPluginKey,
command: ({editor, range, props}) => { command: ({editor, range, props}) => {
const {tag, punctuation} = props
// increase range.to by one when the next node is of type "text" // increase range.to by one when the next node is of type "text"
// and starts with a space character // and starts with a space character
const nodeAfter = editor.view.state.selection.$to.nodeAfter const nodeAfter = editor.view.state.selection.$to.nodeAfter
@@ -130,11 +134,11 @@ export const Tags = Node.create<MentionOptions>({
.insertContentAt(range, [ .insertContentAt(range, [
{ {
type: this.name, type: this.name,
attrs: props, attrs: {id: tag},
}, },
{ {
type: 'text', type: 'text',
text: ' ', text: `${punctuation || ''} `,
}, },
]) ])
.run() .run()
@@ -145,6 +149,7 @@ export const Tags = Node.create<MentionOptions>({
const $from = state.doc.resolve(range.from) const $from = state.doc.resolve(range.from)
const type = state.schema.nodes[this.name] const type = state.schema.nodes[this.name]
const allow = !!$from.parent.type.contentMatch.matchType(type) const allow = !!$from.parent.type.contentMatch.matchType(type)
return allow return allow
}, },
findSuggestionMatch: findSuggestionMatch, findSuggestionMatch: findSuggestionMatch,
@@ -49,6 +49,13 @@ export function findSuggestionMatch({
const from = textFrom + match.index + leadingSpaceOffset const from = textFrom + match.index + leadingSpaceOffset
const to = from + tag.length + hashtagOffset const to = from + tag.length + hashtagOffset
console.log({
pos: $position.pos,
from,
to,
text,
})
// If the $position is located within the matched substring, return that range // If the $position is located within the matched substring, return that range
if (from < $position.pos && to >= $position.pos) { if (from < $position.pos && to >= $position.pos) {
return { return {
@@ -56,7 +63,11 @@ export function findSuggestionMatch({
from, from,
to, to,
}, },
query: tag.replace(TRAILING_PUNCTUATION_REGEX, ''), /**
* TODO
* We parse out the punctuation later.
*/
query: tag,
text: fullMatch.replace(/^\s{1}/, ''), text: fullMatch.replace(/^\s{1}/, ''),
} }
} }
@@ -1,17 +1,16 @@
import React, {forwardRef, useImperativeHandle, useState} from 'react' import React, {forwardRef, useImperativeHandle, useState} from 'react'
import {Pressable, StyleSheet, View} from 'react-native' import {Pressable, StyleSheet, View} from 'react-native'
import {ReactRenderer} from '@tiptap/react' import {ReactRenderer} from '@tiptap/react'
import tippy, {Instance as TippyInstance} from 'tippy.js'
import { import {
SuggestionKeyDownProps,
SuggestionOptions, SuggestionOptions,
SuggestionProps, SuggestionProps,
SuggestionKeyDownProps,
} from '@tiptap/suggestion' } from '@tiptap/suggestion'
import tippy, {Instance as TippyInstance} from 'tippy.js'
// import {TagsAutocompleteModel} from 'state/models/ui/tags-autocomplete' // import {TagsAutocompleteModel} from 'state/models/ui/tags-autocomplete'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
import {Text} from 'view/com/util/text/Text' import {Text} from 'view/com/util/text/Text'
import {parsePunctuationFromTag} from './utils' import {parsePunctuationFromTag} from './utils'
type AutocompleteResult = string type AutocompleteResult = string
@@ -22,9 +21,8 @@ type AutocompleteRef = {
onKeyDown: (props: SuggestionKeyDownProps) => boolean onKeyDown: (props: SuggestionKeyDownProps) => boolean
} }
export function createTagsAutocomplete({ export function createTagsAutocomplete({}: // autocompleteModel,
// autocompleteModel, {
}: {
// autocompleteModel: TagsAutocompleteModel // autocompleteModel: TagsAutocompleteModel
}): Omit<SuggestionOptions, 'editor'> { }): Omit<SuggestionOptions, 'editor'> {
return { return {
@@ -32,9 +30,7 @@ export function createTagsAutocomplete({
* This `query` param comes from the result of `findSuggestionMatch` * This `query` param comes from the result of `findSuggestionMatch`
*/ */
async items({query}) { async items({query}) {
// autocompleteModel.setActive(true) return [query, 'javascript']
// await autocompleteModel.search(query)
return ['tag']//autocompleteModel.suggestions.slice(0, 8)
}, },
render() { render() {
let component: ReactRenderer<AutocompleteRef> | undefined let component: ReactRenderer<AutocompleteRef> | undefined
@@ -45,7 +41,6 @@ export function createTagsAutocomplete({
component = new ReactRenderer(Autocomplete, { component = new ReactRenderer(Autocomplete, {
props: { props: {
...props, ...props,
//autocompleteModel,
}, },
editor: props.editor, editor: props.editor,
}) })
@@ -110,7 +105,6 @@ const Autocomplete = forwardRef<AutocompleteRef, ListProps>(
* separately above. We could do this in `command` definition, but we * separately above. We could do this in `command` definition, but we
* only want to `commitRecentTag` with the sanitized tag. * only want to `commitRecentTag` with the sanitized tag.
*/ */
// @ts-ignore
command({tag, punctuation}) command({tag, punctuation})
}, },
[command], [command],
@@ -141,7 +135,7 @@ const Autocomplete = forwardRef<AutocompleteRef, ListProps>(
if (event.key === 'Enter') { if (event.key === 'Enter') {
if (!props.items.length) { if (!props.items.length) {
// no items, use whatever the user typed // no items, use whatever the user typed
// commit(autocompleteModel.query) commit(props.query)
} else { } else {
selectItem(selectedIndex) selectItem(selectedIndex)
} }
@@ -149,7 +143,7 @@ const Autocomplete = forwardRef<AutocompleteRef, ListProps>(
} }
if (event.key === ' ') { if (event.key === ' ') {
// commit(autocompleteModel.query) commit(props.query)
return true return true
} }