lxsmnsyc / solid-tiptap

SolidJS bindings for tiptap
MIT License
114 stars 3 forks source link

Improvements on the documentation of useEditorIsActive #10

Open kkdaisuki opened 3 months ago

kkdaisuki commented 3 months ago

It appears that in the current implementation, when only one parameter is provided, it would be always interpreted as options.

// This would always return true
useEditorIsActive(editor, 'heading')
// This would work as intended
useEditorIsActive(editor, 'heading', {})

This is different from the official Editor API in the Tiptap documentation where the only parameter may be interpreted either way as options or as the name of the node or mark, i.e.,

// Check if it’s a heading
editor.isActive('heading')
// Check if it’s a heading with a specific attribute value
editor.isActive('heading', { level: 2 })
// Check if it has a specific attribute value, doesn’t care what node/mark it is
editor.isActive({ textAlign: 'justify' })

This behavior is certainly related to https://github.com/lxsmnsyc/solid-tiptap/blob/6b7f27a7430bba6cb5594210cd06fb0bdad30e6b/packages/solid-tiptap/src/Editor.tsx#L84 where the case of only one parameter is defaulted as the options, thus not evaluated.

To make things clearer, this discrepancy would better be either documented or fixed like

// Not tested
export function useEditorIsActive<
  V extends Editor | undefined,
  R extends Record<string, any>,
>(
  editor: () => V,
  ...args: [name: () => string, options?: R] | [options: R] | [name: () => string]
): () => boolean | undefined {
  return createEditorTransaction(editor, instance => {
    if (args.length === 2) {
      return instance?.isActive(args[0](), args[1]);
    }
    return (typeof args[0] === "function") ? instance?.isActive(args[0]()) : instance?.isActive(args[0]);
  });
}