Skip to content

Commit

Permalink
feat: add setMeta command
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Jun 2, 2021
1 parent b3b297f commit 36dad2b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/src/docPages/api/commands/set-meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# setMeta
Store a metadata property in the current transaction.

## Parameters
`key: string`

The name of your metadata. You can get its value at any time with [getMeta](https://prosemirror.net/docs/ref/#state.Transaction.getMeta).

`value: any`

Store any value within your metadata.

## Usage
```js
// Prevent the update event from being triggered
editor.commands.setMeta('preventUpdate', true)

// Store any value in the current transaction.
// You can get this value at any time with tr.getMeta('foo').
editor.commands.setMeta('foo', 'bar')
```
2 changes: 2 additions & 0 deletions docs/src/links.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
- title: setMark
link: /api/commands/set-mark
type: draft
- title: setMeta
link: /api/commands/set-meta
- title: setNode
link: /api/commands/set-node
type: draft
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/commands/setMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Command, RawCommands } from '../types'

declare module '@tiptap/core' {
interface Commands {
setMeta: {
/**
* Store a metadata property in the current transaction.
*/
setMeta: (key: string, value: any) => Command,
}
}
}

export const setMeta: RawCommands['setMeta'] = (key, value) => ({ tr }) => {
tr.setMeta(key, value)

return true
}
3 changes: 3 additions & 0 deletions packages/core/src/extensions/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import * as selectNodeForward from '../commands/selectNodeForward'
import * as selectParentNode from '../commands/selectParentNode'
import * as setContent from '../commands/setContent'
import * as setMark from '../commands/setMark'
import * as setMeta from '../commands/setMeta'
import * as setNode from '../commands/setNode'
import * as setNodeSelection from '../commands/setNodeSelection'
import * as setTextSelection from '../commands/setTextSelection'
Expand Down Expand Up @@ -78,6 +79,7 @@ export { selectNodeForward }
export { selectParentNode }
export { setContent }
export { setMark }
export { setMeta }
export { setNode }
export { setNodeSelection }
export { setTextSelection }
Expand Down Expand Up @@ -131,6 +133,7 @@ export const Commands = Extension.create({
...selectParentNode,
...setContent,
...setMark,
...setMeta,
...setNode,
...setNodeSelection,
...setTextSelection,
Expand Down

0 comments on commit 36dad2b

Please sign in to comment.