Skip to content

Commit

Permalink
feat: move types into core package
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Dec 31, 2024
1 parent 24c1009 commit 286fa0c
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 78 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import { style } from './style.js'
import {
CanCommands,
ChainedCommands,
DocumentType,
EditorEvents,
EditorOptions,
JSONContent,
NodeType as TNodeType,
SingleCommands,
TextSerializer,
TextType as TTextType,
} from './types.js'
import { createStyleTag } from './utilities/createStyleTag.js'
import { isFunction } from './utilities/isFunction.js'
Expand Down Expand Up @@ -533,7 +535,10 @@ export class Editor extends EventEmitter<EditorEvents> {
/**
* Get the document as JSON.
*/
public getJSON(): JSONContent {
public getJSON(): DocumentType<
Record<string, any> | undefined,
TNodeType<string, undefined | Record<string, any>, any, (TNodeType | TTextType)[]>[]
> {
return this.state.doc.toJSON()
}

Expand Down
55 changes: 55 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,61 @@ export type JSONContent = {
[key: string]: any;
};

/**
* A mark type is either a JSON representation of a mark or a Prosemirror mark instance
*/
export type MarkType<
Type extends string | { name: string } = any,
Attributes extends undefined | Record<string, any> = any,
> = {
type: Type;
attrs: Attributes;
};

/**
* A node type is either a JSON representation of a node or a Prosemirror node instance
*/
export type NodeType<
Type extends string | { name: string } = any,
Attributes extends undefined | Record<string, any> = any,
NodeMarkType extends MarkType = any,
Content extends (NodeType | TextType)[] = any,
> = {
type: Type;
attrs: Attributes;
content?: Content;
marks?: NodeMarkType[];
};

/**
* A node type is either a JSON representation of a doc node or a Prosemirror doc node instance
*/
export type DocumentType<
TDocAttributes extends Record<string, any> | undefined = Record<string, any>,
TContentType extends NodeType[] = NodeType[],
> = Omit<NodeType<'doc', TDocAttributes, never, TContentType>, 'marks' | 'content'> & {content: TContentType};

/**
* A node type is either a JSON representation of a text node or a Prosemirror text node instance
*/
export type TextType<TMarkType extends MarkType = MarkType> = {
type: 'text';
text: string;
marks: TMarkType[];
};

/**
* Describes the output of a `renderHTML` function in prosemirror
* @see https://prosemirror.net/docs/ref/#model.DOMOutputSpec
*/
export type DOMOutputSpecArray =
| [string]
| [string, Record<string, any>]
| [string, 0]
| [string, Record<string, any>, 0]
| [string, Record<string, any>, DOMOutputSpecArray | 0]
| [string, DOMOutputSpecArray];

export type Content = HTMLContent | JSONContent | JSONContent[] | null;

export type CommandProps = {
Expand Down
7 changes: 4 additions & 3 deletions packages/static-renderer/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ExtensionAttribute, mergeAttributes } from '@tiptap/core'

import type { MarkType, NodeType } from './types'
import {
type ExtensionAttribute, type MarkType, type NodeType,
mergeAttributes,
} from '@tiptap/core'

/**
* This function returns the attributes of a node or mark that are defined by the given extension attributes.
Expand Down
1 change: 0 additions & 1 deletion packages/static-renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export * from './json/html-string/index.js'
export * from './json/react/index.js'
export * from './pm/html-string/index.js'
export * from './pm/react/index.js'
export * from './types.js'
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { TextType } from '@tiptap/core'

import { renderJSONContentToString } from './string.js'

/**
Expand All @@ -14,8 +16,7 @@ console.log(
renderJSONContentToString({
nodeMapping: {
text({ node }) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return node.text!
return (node as unknown as TextType).text
},
heading({ node, children }) {
const level = node.attrs?.level
Expand Down
3 changes: 2 additions & 1 deletion packages/static-renderer/src/json/html-string/string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { MarkType, NodeType } from '../../types.js'
import type { MarkType, NodeType } from '@tiptap/core'

import { TiptapStaticRenderer, TiptapStaticRendererOptions } from '../renderer.js'

export function renderJSONContentToString<
Expand Down
4 changes: 2 additions & 2 deletions packages/static-renderer/src/json/react/react.example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NodeType, TextType } from '@tiptap/core'
import React from 'react'

import { NodeType } from '../../types.js'
import { NodeProps } from '../renderer.js'
import { renderJSONContentToReactElement } from './react.js'

Expand All @@ -17,7 +17,7 @@ import { renderJSONContentToReactElement } from './react.js'
console.log(renderJSONContentToReactElement({
nodeMapping: {
text({ node }) {
return node.text ?? null
return (node as unknown as TextType).text ?? null
},
heading({
node,
Expand Down
2 changes: 1 addition & 1 deletion packages/static-renderer/src/json/react/react.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { MarkType, NodeType } from '@tiptap/core'
import React from 'react'

import { MarkType, NodeType } from '../../types.js'
import { TiptapStaticRenderer, TiptapStaticRendererOptions } from '../renderer.js'

export function renderJSONContentToReactElement<
Expand Down
10 changes: 5 additions & 5 deletions packages/static-renderer/src/json/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { MarkType, NodeType } from '../types'
import type { MarkType, NodeType } from '@tiptap/core'

/**
* Props for a node renderer
Expand Down Expand Up @@ -87,19 +87,19 @@ export type TiptapStaticRendererOptions<
/**
* Mapping of node types to react components
*/
nodeMapping: Record<string, TNodeRender>;
nodeMapping: Record<string, NoInfer<TNodeRender>>;
/**
* Mapping of mark types to react components
*/
markMapping: Record<string, TMarkRender>;
markMapping: Record<string, NoInfer<TMarkRender>>;
/**
* Component to render if a node type is not handled
*/
unhandledNode?: TNodeRender;
unhandledNode?: NoInfer<TNodeRender>;
/**
* Component to render if a mark type is not handled
*/
unhandledMark?: TMarkRender;
unhandledMark?: NoInfer<TMarkRender>;
};

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/static-renderer/src/pm/html-string/html-string.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Extensions, JSONContent } from '@tiptap/core'
import type { DOMOutputSpecArray, Extensions, JSONContent } from '@tiptap/core'
import type { DOMOutputSpec, Mark, Node } from '@tiptap/pm/model'

import { renderJSONContentToString } from '../../json/html-string/string.js'
import { TiptapStaticRendererOptions } from '../../json/renderer.js'
import type { DOMOutputSpecArray } from '../../types.js'
import { renderToElement } from '../extensionRenderer.js'

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/static-renderer/src/pm/react/react.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable no-plusplus, @typescript-eslint/no-explicit-any */
import { Extensions, JSONContent } from '@tiptap/core'
import type { DOMOutputSpecArray, Extensions, JSONContent } from '@tiptap/core'
import type { DOMOutputSpec, Mark, Node } from '@tiptap/pm/model'
import React from 'react'

import { renderJSONContentToReactElement } from '../../json/react/react.js'
import { TiptapStaticRendererOptions } from '../../json/renderer.js'
import type { DOMOutputSpecArray } from '../../types.js'
import { renderToElement } from '../extensionRenderer.js'

/**
Expand Down
57 changes: 0 additions & 57 deletions packages/static-renderer/src/types.ts

This file was deleted.

0 comments on commit 286fa0c

Please sign in to comment.