Skip to content

Commit

Permalink
removed name attribute from mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
iskaktoltay committed Nov 29, 2024
1 parent 98b1c6e commit 050d756
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 87 deletions.
12 changes: 3 additions & 9 deletions frontend/apps/desktop/src/editor/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ export function createAutoCompletePlugin<N extends string, T>(args: {

const onCreate = (
link: string,
name: string,
range: {from: number; to: number},
) => {
const node = view.state.schema.nodes[nodeName].create({
link,
name,
})
view.dispatch(
view.state.tr.replaceWith(
Expand Down Expand Up @@ -359,7 +357,7 @@ function AutocompletePopupInner(
) {
let item = suggestions[group][idx]

onCreate(item.value, item.title, range)
onCreate(item.value, range)
onClose()
}
return true
Expand Down Expand Up @@ -469,7 +467,7 @@ function AutocompletePopupInner(
// setIndex(i)
}}
onPress={() => {
onCreate(item.value, item.title, range)
onCreate(item.value, range)
onClose()
}}
/>
Expand Down Expand Up @@ -515,11 +513,7 @@ export type AutocompleteTokenPluginActiveState<T> = {
}

export type AutocompleteTokenPluginActions = {
onCreate: (
link: string,
name: string,
range: {from: number; to: number},
) => void
onCreate: (link: string, range: {from: number; to: number}) => void
onClose: () => void
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export function inlineContentToNodes(
nodes.push(
schema.nodes['inline-embed'].create({
link: content.link,
name: content.name,
}),
)
} else {
Expand Down Expand Up @@ -234,7 +233,6 @@ function contentNodeToInlineContent(contentNode: Node) {
content.push({
type: node.type.name,
link: node.attrs.link,
name: node.attrs.name,
})

currentContent = undefined
Expand Down Expand Up @@ -412,10 +410,7 @@ export function nodeToBlock<BSchema extends BlockSchema>(
})) {
const blockSpec = blockSchema[blockInfo.contentType.name]
if (!blockSpec) {
if (
blockInfo.contentType.name === 'code-block' ||
blockInfo.contentType.name === 'inline-embed'
) {
if (blockInfo.contentType.name === 'code-block') {
break
} else
throw Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type BNLink = {
export type InlineEmbed = {
type: 'inline-embed'
link: string
name: string
}

export type PartialLink = Omit<BNLink, 'content'> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ class HyperlinkToolbarView<BSchema extends BlockSchema> {
: this.pmView.state.selection.from
tr = tr.setNodeMarkup(pos, null, {
link: url,
name: text,
})
// return
} else {
Expand Down Expand Up @@ -237,7 +236,6 @@ class HyperlinkToolbarView<BSchema extends BlockSchema> {
: this.pmView.state.selection.from
tr = tr.setNodeMarkup(pos, null, {
link: url,
name: text,
})
// return
} else {
Expand Down Expand Up @@ -422,9 +420,7 @@ class HyperlinkToolbarView<BSchema extends BlockSchema> {
this.hyperlinkMarkRange!.to,
),
url: this.hyperlinkMark!.attrs.link,
text: this.hyperlinkMark!.attrs.name
? this.hyperlinkMark!.attrs.name
: ' ',
text: ' ',
type: 'mention',
id: container ? container.attrs.id : '',
}
Expand Down
6 changes: 2 additions & 4 deletions frontend/apps/desktop/src/editor/hm-link-switch-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function HypermediaLinkSwitchToolbar(
editLink={props.editHyperlink}
openUrl={props.openUrl}
type={props.type}
hasName={props.type !== 'embed'}
hasName={props.type !== 'embed' && props.type !== 'mention'}
hasSearch={props.type !== 'link'}
isSeedDocument={unpackedRef ? true : false}
/>
Expand Down Expand Up @@ -132,7 +132,7 @@ export function HypermediaLinkSwitchToolbar(
$pos.start() + offset + 1,

state.schema.text(
mention.attrs.name,
mention.attrs.link,
// @ts-ignore
state.schema.marks['link'].create({href: props.url})!,
),
Expand Down Expand Up @@ -175,7 +175,6 @@ export function HypermediaLinkSwitchToolbar(
const node = state.schema.nodes['inline-embed'].create(
{
link: props.url,
name: props.text,
},
state.schema.text(' '),
)
Expand All @@ -186,7 +185,6 @@ export function HypermediaLinkSwitchToolbar(
content: [],
props: {
link: props.url,
name: props.text ? props.text : props.url,
},
} as PartialBlock<HMBlockSchema>
// props.editor.insertBlocks([mentionBlock], props.id, 'after')
Expand Down
4 changes: 1 addition & 3 deletions frontend/apps/desktop/src/editor/hyperlink-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ export function HypermediaLinkToolbar(
editLink={props.editHyperlink}
openUrl={props.openUrl}
type={props.type}
hasName={true}
hasName={props.type !== 'mention'}
hasSearch={props.type === 'mention'}
isSeedDocument={props.isSeedDocument}
isFocused={props.isFocused}
setIsFocused={props.setIsFocused}
/>
</YStack>
)
Expand Down
42 changes: 10 additions & 32 deletions frontend/apps/desktop/src/editor/mentions-plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ export function createInlineEmbedNode(bnEditor: any) {
link: {
default: '',
},
name: {
default: '',
},
}
},
addProseMirrorPlugins() {
Expand All @@ -83,24 +80,13 @@ function InlineEmbedNodeComponent(props: any) {
<NodeViewWrapper
className={`inline-embed-token${props.selected ? ' selected' : ''}`}
data-inline-embed={props.node.attrs.link}
data-name={props.node.attrs.name}
>
<MentionToken
value={props.node.attrs.link}
name={props.node.attrs.name}
selected={props.selected}
updateAttributes={props.updateAttributes}
/>
<MentionToken value={props.node.attrs.link} selected={props.selected} />
</NodeViewWrapper>
)
}

export function MentionToken(props: {
value: string
name: string
selected?: boolean
updateAttributes: (attributes: {[key: string]: any}) => void
}) {
export function MentionToken(props: {value: string; selected?: boolean}) {
const unpackedRef = unpackHmId(props.value)

if (unpackedRef?.type == 'd') {
Expand All @@ -113,28 +99,20 @@ export function MentionToken(props: {

function DocumentMention({
unpackedRef,
name,
selected,
updateAttributes,
}: {
unpackedRef: UnpackedHypermediaId
name: string
selected?: boolean
updateAttributes: (attributes: {[key: string]: any}) => void
}) {
let mentionName = name
if (!mentionName) {
const entity = useEntity(unpackedRef)
const docTitle = entity.data?.document
? getDocumentTitle(entity.data?.document)
: unpackedRef.id
const entity = useEntity(unpackedRef)

if (!mentionName && docTitle) {
updateAttributes({name: docTitle})
}
}

return <MentionText selected={selected}>{mentionName}</MentionText>
return (
<MentionText selected={selected}>
{entity.data?.document
? getDocumentTitle(entity.data?.document)
: unpackedRef.id}
</MentionText>
)
}

export function MentionText(props: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ describe('EditorBlock to HMBlock', () => {
type: 'inline-embed',
link: 'hm://asdf1234',
styles: {},
name: 'test embed',
},
],
}
Expand All @@ -294,9 +293,7 @@ describe('EditorBlock to HMBlock', () => {
annotations: [
{
type: 'Embed',
attributes: {
name: 'test embed',
},
attributes: {},
starts: [6],
ends: [7],
link: 'hm://asdf1234',
Expand Down Expand Up @@ -325,7 +322,6 @@ describe('EditorBlock to HMBlock', () => {
type: 'inline-embed',
link: 'hm://asdf1234',
styles: {},
name: 'test embed',
},
{
type: 'text',
Expand Down Expand Up @@ -361,9 +357,7 @@ describe('EditorBlock to HMBlock', () => {
annotations: [
{
type: 'Embed',
attributes: {
name: 'test embed',
},
attributes: {},
starts: [6],
ends: [7],
link: 'hm://asdf1234',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ describe('HMBlock to EditorBlock', () => {
{
type: 'Embed',
starts: [6],
attributes: {
name: 'test embed',
},
attributes: {},
ends: [7],
link: 'hm://asdf1234',
},
Expand All @@ -319,7 +317,6 @@ describe('HMBlock to EditorBlock', () => {
type: 'inline-embed',
link: 'hm://asdf1234',
styles: {},
name: 'test embed',
},
],
}
Expand All @@ -336,9 +333,7 @@ describe('HMBlock to EditorBlock', () => {
{
type: 'Embed',
starts: [6],
attributes: {
name: 'test embed',
},
attributes: {},
ends: [7],
link: 'hm://asdf1234',
},
Expand Down Expand Up @@ -377,7 +372,6 @@ describe('HMBlock to EditorBlock', () => {
type: 'inline-embed',
link: 'hm://asdf1234',
styles: {},
name: 'test embed',
},
{
type: 'text',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ export function editorBlockToHMBlock(editorBlock: EditorBlock): HMBlock {
}

if (leaf.type == 'inline-embed') {
annotations.addSpan(
'Embed',
{link: leaf.link, name: leaf.name},
start,
end,
)
annotations.addSpan('Embed', {link: leaf.link}, start, end)
}

if (leaf.type == 'link') {
Expand Down Expand Up @@ -214,7 +209,6 @@ function flattenLeaves(
result.push({
...leaf,
text: '\uFFFC',
name: leaf.name,
link: leaf.link,
} as const)
}
Expand Down
2 changes: 0 additions & 2 deletions frontend/packages/shared/src/client/hmblock-to-editorblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ export function hmBlockToEditorBlock(block: HMBlock): EditorBlock {
if (l.type == 'Embed') {
linkAnnotation = {
type: 'inline-embed',
name: l.attributes.name,
link: l.link,
}
}
Expand All @@ -298,7 +297,6 @@ export function hmBlockToEditorBlock(block: HMBlock): EditorBlock {
leaves.push({
type: 'inline-embed',
styles: {},
name: linkAnnotation.name,
link: linkAnnotation.link,
} as EditorInlineEmbed)
textStart = i + 1
Expand Down
1 change: 0 additions & 1 deletion frontend/packages/shared/src/editor-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export interface EditorLink {
export interface EditorInlineEmbed {
type: 'inline-embed'
link: string
name: string
styles: EditorInlineStyles | {}
}

Expand Down
1 change: 0 additions & 1 deletion frontend/packages/shared/src/hm-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export const InlineEmbedAnnotationSchema = z
.object({
type: z.literal('Embed'),
...baseAnnotationProperties,
attributes: z.object({name: z.string().optional()}),
link: z.string(),
})
.strict()
Expand Down

0 comments on commit 050d756

Please sign in to comment.