extenify-dev
v0.0.5
Published
A tiptap extendable implement
Readme
extenify-dev
extenify is a general extension kit that can be use to enhance host with the ability to work with extensions.things like I can wrap tanstack table and extenify into a new tiptable with extensions features work with.
Usage
import { createTable, getCoreRowModel, getSortedRowModel } from "@tanstack/table-core"
import { Extension, Extenify, provide } from "extenify-dev"
declare module "extenify-dev" {
export interface Lifecycle {
onBeforeCreate?(this: { name: string }): void
}
}
const SortExtension = Extension.create({
name: "extension.sort",
defaultOptions: {
docs: `defaultOptions is Extension internal options, can be override by Extension.configure()`,
},
// finally will be merged in Extenify with property extenify.options to provide to the host
addOptions() {
console.log("addOptions")
return {
state: {
sorting: [],
},
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
}
},
// for Extension internal storage(state)
addStorage() {
console.log("addStorage")
return {
sort: [],
}
},
addCommands({ table }) {
console.log("addCommands", this.storage.sort)
return {
toggleSort: () => {
// use context
table.toggleSort()
// or use this scope
this.table.toggleSort()
},
}
},
addKeyboardShortcuts({ commands }) {
console.log("addKeyboardShortcuts")
return {
"Mod-k": () => {
// use context
commands.toggleSort()
// or use this scope
this.commands.toggleSort()
return false
},
}
},
onBeforeCreate() {
console.log("onBeforeCreate", this.name)
},
})
const table = createTable()
provide("table", table)
// or
provide({
table: table,
editor: editor,
})
const extenify = Extenify.create({
extensions: [
// .configure() only merge defaultOptions and return the same instance(NOT a new instance)
SortExtension.configure({
usage: `this will merge the defaultOptions in SortExtension internal`,
}),
// .extend() will merge all properties and return a new instance
SortExtension.extend({
name: ``,
defaultOptions: {
usage: `all properties in SortExtension.extend will merge the internal properties`,
},
})
],
})
console.log(extenify.extensions) // all extensions
extenify.commands.toggleSort() // with type safeprovide({
table: table,
editor: editor,
})
Extension.create((context) => {
const { table, editor } = context
return {
addOptions(context) {
const { table, editor } = context
},
addStorage(context) {
const { table, editor } = context
},
addCommands(context) {
const { table, editor } = context
},
addKeyboardShortcuts(context) {
const { table, editor } = context
},
}
})
Extension.create({
addOptions(context) {
const { table, editor } = context
},
addStorage(context) {
const { table, editor } = context
},
addCommands(context) {
const { table, editor } = context
},
addKeyboardShortcuts(context) {
const { table, editor } = context
},
})