Skip to content

Commit

Permalink
fix added space on commit
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Oct 10, 2023
1 parent 5033520 commit f97f130
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/view/com/composer/text-input/web/Tags/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const Tags = Node.create<TagOptions>({
allowSpaces: true,
pluginKey: TagsPluginKey,
command: ({editor, range, props}) => {
const {tag, punctuation} = props

// increase range.to by one when the next node is of type "text"
// and starts with a space character
const nodeAfter = editor.view.state.selection.$to.nodeAfter
Expand All @@ -42,11 +44,11 @@ export const Tags = Node.create<TagOptions>({
.insertContentAt(range, [
{
type: this.name,
attrs: props,
attrs: {id: tag},
},
{
type: 'text',
text: ' ',
text: `${punctuation || ''} `,
},
])
.run()
Expand Down Expand Up @@ -92,8 +94,12 @@ export const Tags = Node.create<TagOptions>({

const from = startIndex + match.index + matchedString.indexOf(tag)
// `to` should not include ending punctuation
const to = from + tagWithoutPunctuation.length
const to = from + tag.length

console.log({
from,
to,
})
if (
from < cursorPosition &&
to >= cursorPosition - punctuationIndexOffset
Expand All @@ -103,8 +109,12 @@ export const Tags = Node.create<TagOptions>({
from,
to,
},
// should not include ending punctuation
query: tagWithoutPunctuation.replace(/^#/, ''),
/**
* This is passed to the `items({ query })` method configured in `createTagsAutocomplete`.
*
* TODO This value should follow the rules of our hashtags spec.
*/
query: tag.replace(/^#/, ''),
// raw text string
text: matchedString,
}
Expand Down
19 changes: 16 additions & 3 deletions src/view/com/composer/text-input/web/Tags/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export function createTagsAutocomplete({
autocompleteModel: TagsAutocompleteModel
}): Omit<SuggestionOptions, 'editor'> {
return {
/**
* This `query` param comes from the result of `findSuggestionMatch`
*/
async items({query}) {
autocompleteModel.setActive(true)
await autocompleteModel.search(query)
Expand Down Expand Up @@ -95,9 +98,19 @@ const Autocomplete = forwardRef<AutocompleteRef, ListProps>(
const [selectedIndex, setSelectedIndex] = useState(0)

const commit = React.useCallback(
(tag: string) => {
// @ts-ignore we're dealing with strings here not mentions
command({id: tag})
(query: string) => {
const tag = query.replace(/(\p{P}+)$/gu, '')
const punctuation = query.match(/(\p{P}+)$/gu)?.[0] || ''
/*
* This values here are passed directly to the `command` method
* configured in the `Tags` plugin.
*
* The type error is ignored because we parse the tag and punctuation
* separately above. We could do this in `command` definition, but we
* only want to `commitRecentTag` with the sanitized tag.
*/
// @ts-ignore
command({tag, punctuation})
autocompleteModel.commitRecentTag(tag)
},
[command, autocompleteModel],
Expand Down

0 comments on commit f97f130

Please sign in to comment.