Skip to content

Commit

Permalink
feat: init tiptap-extension-unique-id
Browse files Browse the repository at this point in the history
  • Loading branch information
litingyes committed Mar 1, 2024
1 parent cc4fc8a commit 418406d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 6 deletions.
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@tiptap/core": "^2.2.4",
"vue": "^3.3.4"
},
"devDependencies": {
Expand Down
14 changes: 13 additions & 1 deletion packages/tiptap-extension-unique-id/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
"name": "@node-editor/tiptap-extension-unique-id",
"type": "module",
"version": "0.0.0",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"dependencies": {},
"peerDependencies": {
"@tiptap/core": "^2.2"
},
"dependencies": {
"nanoid": "^5.0.6"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
Expand Down
2 changes: 1 addition & 1 deletion packages/tiptap-extension-unique-id/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/tiptap-extension-unique-id'
export * from './uniqueId'

This file was deleted.

39 changes: 39 additions & 0 deletions packages/tiptap-extension-unique-id/src/uniqueId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Extension } from '@tiptap/core'
import { nanoid } from 'nanoid'

export interface UniqueIdOptions {
attributeName?: string
types?: string[]
generateID?: () => string
}

export const uniqueId = Extension.create<UniqueIdOptions>({
name: 'uniqueId',
addGlobalAttributes() {
let { attributeName, types, generateID } = this.options

attributeName ??= 'id'
types ??= ['paragraph']
generateID ??= () => nanoid()

return [
{
types,
attributes: {
[attributeName]: {
default: generateID(),
rendered: true,
isRequired: true,
keepOnSplit: true,
parseHTML: element => element.getAttribute(`data-${attributeName}`),
renderHTML: (attributes) => {
return {
[`data-${attributeName}`]: attributes[attributeName!],
}
},
},
},
},
]
},
})
2 changes: 1 addition & 1 deletion packages/tiptap-extension-unique-id/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default defineConfig({
},
rollupOptions: {
// External packages that should not be bundled into your library.
external: [],
external: ['@tiptap/core'],
},
},
})
2 changes: 2 additions & 0 deletions packages/vue-play/src/app/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { Editor, EditorContent } from '@tiptap/vue-3'
import StarterKit from '@tiptap/starter-kit'
import { uniqueId } from '@node-editor/tiptap-extension-unique-id'
import { onBeforeUnmount, onMounted, shallowRef } from 'vue'
const editor = shallowRef<Editor>()
Expand All @@ -9,6 +10,7 @@ onMounted(() => {
content: 'Node Editor',
extensions: [
StarterKit,
uniqueId,
],
})
})
Expand Down

0 comments on commit 418406d

Please sign in to comment.