New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@milkdown/plugin-listener

Package Overview
Dependencies
Maintainers
1
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@milkdown/plugin-listener - npm Package Compare versions

Comparing version 5.3.1 to 5.3.2

37

lib/index.d.ts

@@ -1,12 +0,31 @@

/// <reference types="prosemirror-model" />
import { MilkdownPlugin } from '@milkdown/core';
import { Ctx, MilkdownPlugin } from '@milkdown/core';
import { Node as ProseNode } from '@milkdown/prose';
export declare type DocListener = (doc: ProseNode) => void;
export declare type MarkdownListener = (getMarkdown: () => string) => void;
export declare type Listener = {
doc?: DocListener[];
markdown?: MarkdownListener[];
};
export declare const listenerCtx: import("@milkdown/core").Slice<Listener>;
declare class ListenerManager {
private beforeMountedListeners;
private mountedListeners;
private updatedListeners;
private markdownUpdatedListeners;
private blurListeners;
private focusListeners;
private destroyListeners;
get listeners(): {
beforeMounted: ((ctx: Ctx) => void)[];
mounted: ((ctx: Ctx) => void)[];
updated: ((ctx: Ctx, doc: ProseNode<any>, prevDoc: ProseNode<any> | null) => void)[];
markdownUpdated: ((ctx: Ctx, markdown: string, prevMarkdown: string | null) => void)[];
blur: ((ctx: Ctx) => void)[];
focus: ((ctx: Ctx) => void)[];
destroy: ((ctx: Ctx) => void)[];
};
beforeMount: (fn: (ctx: Ctx) => void) => this;
mounted: (fn: (ctx: Ctx) => void) => this;
updated: (fn: (ctx: Ctx, doc: ProseNode, prevDoc: ProseNode | null) => void) => this;
markdownUpdated(fn: (ctx: Ctx, markdown: string, prevMarkdown: string | null) => void): this;
blur(fn: (ctx: Ctx) => void): this;
focus(fn: (ctx: Ctx) => void): this;
destroy(fn: (ctx: Ctx) => void): this;
}
export declare const listenerCtx: import("@milkdown/core").Slice<ListenerManager>;
export declare const listener: MilkdownPlugin;
export {};
//# sourceMappingURL=index.d.ts.map

@@ -1,11 +0,88 @@

import { createSlice, SerializerReady, serializerCtx, prosePluginsCtx } from "@milkdown/core";
import { createSlice, InitReady, SerializerReady, serializerCtx, prosePluginsCtx, EditorViewReady } from "@milkdown/core";
import { Plugin } from "@milkdown/prose";
const listenerCtx = createSlice({}, "listener");
class ListenerManager {
constructor() {
this.beforeMountedListeners = [];
this.mountedListeners = [];
this.updatedListeners = [];
this.markdownUpdatedListeners = [];
this.blurListeners = [];
this.focusListeners = [];
this.destroyListeners = [];
this.beforeMount = (fn) => {
this.beforeMountedListeners.push(fn);
return this;
};
this.mounted = (fn) => {
this.mountedListeners.push(fn);
return this;
};
this.updated = (fn) => {
this.updatedListeners.push(fn);
return this;
};
}
get listeners() {
return {
beforeMounted: this.beforeMountedListeners,
mounted: this.mountedListeners,
updated: this.updatedListeners,
markdownUpdated: this.markdownUpdatedListeners,
blur: this.blurListeners,
focus: this.focusListeners,
destroy: this.destroyListeners
};
}
markdownUpdated(fn) {
this.markdownUpdatedListeners.push(fn);
return this;
}
blur(fn) {
this.blurListeners.push(fn);
return this;
}
focus(fn) {
this.focusListeners.push(fn);
return this;
}
destroy(fn) {
this.destroyListeners.push(fn);
return this;
}
}
const listenerCtx = createSlice(new ListenerManager(), "listener");
const listener = (pre) => {
pre.inject(listenerCtx);
pre.inject(listenerCtx, new ListenerManager());
return async (ctx) => {
await ctx.wait(InitReady);
const listener2 = ctx.get(listenerCtx);
if (listener2.doc || listener2.markdown) {
throw new Error("listener.doc and listener.markdown are deprecated, use new listener manager API instead");
}
const { listeners } = listener2;
listeners.beforeMounted.forEach((fn) => fn(ctx));
await ctx.wait(SerializerReady);
const listener2 = ctx.get(listenerCtx);
const serializer = ctx.get(serializerCtx);
let prevDoc = null;
let prevMarkdown = null;
const plugin = new Plugin({
view: () => {
return {
destroy: () => {
listeners.destroy.forEach((fn) => fn(ctx));
}
};
},
props: {
handleDOMEvents: {
focus: () => {
listeners.focus.forEach((fn) => fn(ctx));
return false;
},
blur: () => {
listeners.blur.forEach((fn) => fn(ctx));
return false;
}
}
},
state: {

@@ -15,11 +92,20 @@ init: () => {

apply: (tr) => {
var _a, _b;
if (!tr.docChanged)
return;
(_a = listener2.markdown) == null ? void 0 : _a.forEach((markdownListener) => {
markdownListener(() => serializer(tr.doc));
});
(_b = listener2.doc) == null ? void 0 : _b.forEach((docListener) => {
docListener(tr.doc);
});
const { doc } = tr;
if (listeners.updated.length > 0 && (prevDoc == null || prevDoc !== doc)) {
listeners.updated.forEach((fn) => {
fn(ctx, doc, prevDoc);
});
}
if (listeners.markdownUpdated.length > 0) {
const markdown = serializer(tr.doc);
if (prevMarkdown == null || prevMarkdown !== markdown) {
listeners.markdownUpdated.forEach((fn) => {
fn(ctx, markdown, prevMarkdown);
});
prevMarkdown = markdown;
}
}
prevDoc = doc;
}

@@ -29,2 +115,4 @@ }

ctx.update(prosePluginsCtx, (x) => x.concat(plugin));
await ctx.wait(EditorViewReady);
listeners.mounted.forEach((fn) => fn(ctx));
};

@@ -31,0 +119,0 @@ };

{
"name": "@milkdown/plugin-listener",
"version": "5.3.1",
"version": "5.3.2",
"type": "module",

@@ -17,16 +17,10 @@ "main": "./lib/index.es.js",

],
"peerDependencies": {
"@milkdown/core": "*"
},
"dependencies": {
"@milkdown/core": "5.3.2",
"@milkdown/prose": "5.3.2",
"@milkdown/utils": "5.3.2",
"tslib": "^2.3.1"
},
"devDependencies": {
"@milkdown/core": "5.3.1",
"@milkdown/prose": "5.3.1",
"@milkdown/utils": "5.3.1"
},
"scripts": {
"start": "vite",
"watch": "vite build --watch",
"start": "vite build --watch",
"test": "vitest",

@@ -36,3 +30,3 @@ "tsc": "tsc --noEmit",

},
"readme": "# @milkdown/plugin-listener\n\nListener plugin for [milkdown](https://saul-mirone.github.io/milkdown/).\n\n# Example Usage\n\n```typescript\nimport { Editor } from '@milkdown/core';\nimport { commonmark } from '@milkdown/preset-commonmark';\nimport { nord } from '@milkdown/theme-nord';\n\nimport { listener, listenerCtx } from '@milkdown/plugin-listener';\n\nEditor.make()\n .config((ctx) => {\n ctx.set(listenerCtx, {\n markdown: [(get) => console.log(get())],\n doc: [console.log],\n });\n })\n .use(nord)\n .use(commonmark)\n .use(listener)\n .create();\n```\n\n# License\n\nMilkdown is open sourced software licensed under [MIT license](https://github.com/Saul-Mirone/milkdown/blob/main/LICENSE).\n"
"readme": "# @milkdown/plugin-listener\n\nListener plugin for [milkdown](https://saul-mirone.github.io/milkdown/).\n\n# Example Usage\n\n```typescript\nimport { Editor } from '@milkdown/core';\nimport { commonmark } from '@milkdown/preset-commonmark';\nimport { nord } from '@milkdown/theme-nord';\n\nimport { listener, listenerCtx } from '@milkdown/plugin-listener';\n\nEditor.make()\n .config((ctx) => {\n ctx.get(listenerCtx)\n .beforeMount((ctx) => {\n // before the editor mounts\n })\n .mounted((ctx) => {\n // after the editor mounts\n })\n .updated((ctx, doc, prevDoc) => {\n // when editor state updates\n })\n .markdownUpdated((ctx, markdown, prevMarkdown) => {\n // when markdown updates\n })\n .blur((ctx) => {\n // when editor loses focus\n })\n .focus((ctx) => {\n // when focus editor\n })\n .destroy((ctx) => {\n // when editor is being destroyed\n });\n })\n .use(nord)\n .use(commonmark)\n .use(listener)\n .create();\n```\n\n# License\n\nMilkdown is open sourced software licensed under [MIT license](https://github.com/Saul-Mirone/milkdown/blob/main/LICENSE).\n"
}

@@ -16,6 +16,24 @@ # @milkdown/plugin-listener

.config((ctx) => {
ctx.set(listenerCtx, {
markdown: [(get) => console.log(get())],
doc: [console.log],
});
ctx.get(listenerCtx)
.beforeMount((ctx) => {
// before the editor mounts
})
.mounted((ctx) => {
// after the editor mounts
})
.updated((ctx, doc, prevDoc) => {
// when editor state updates
})
.markdownUpdated((ctx, markdown, prevMarkdown) => {
// when markdown updates
})
.blur((ctx) => {
// when editor loses focus
})
.focus((ctx) => {
// when focus editor
})
.destroy((ctx) => {
// when editor is being destroyed
});
})

@@ -22,0 +40,0 @@ .use(nord)

/* Copyright 2021, Milkdown by Mirone. */
import { createSlice, MilkdownPlugin, prosePluginsCtx, serializerCtx, SerializerReady } from '@milkdown/core';
import { Node as ProseNode, Plugin as StatePlugin } from '@milkdown/prose';
import {
createSlice,
Ctx,
EditorViewReady,
InitReady,
MilkdownPlugin,
prosePluginsCtx,
serializerCtx,
SerializerReady,
} from '@milkdown/core';
import { Node as ProseNode, Plugin } from '@milkdown/prose';
export type DocListener = (doc: ProseNode) => void;
export type MarkdownListener = (getMarkdown: () => string) => void;
export type Listener = {
doc?: DocListener[];
markdown?: MarkdownListener[];
};
export const listenerCtx = createSlice<Listener>({}, 'listener');
class ListenerManager {
private beforeMountedListeners: Array<(ctx: Ctx) => void> = [];
private mountedListeners: Array<(ctx: Ctx) => void> = [];
private updatedListeners: Array<(ctx: Ctx, doc: ProseNode, prevDoc: ProseNode | null) => void> = [];
private markdownUpdatedListeners: Array<(ctx: Ctx, markdown: string, prevMarkdown: string | null) => void> = [];
private blurListeners: Array<(ctx: Ctx) => void> = [];
private focusListeners: Array<(ctx: Ctx) => void> = [];
private destroyListeners: Array<(ctx: Ctx) => void> = [];
get listeners() {
return {
beforeMounted: this.beforeMountedListeners,
mounted: this.mountedListeners,
updated: this.updatedListeners,
markdownUpdated: this.markdownUpdatedListeners,
blur: this.blurListeners,
focus: this.focusListeners,
destroy: this.destroyListeners,
};
}
beforeMount = (fn: (ctx: Ctx) => void) => {
this.beforeMountedListeners.push(fn);
return this;
};
mounted = (fn: (ctx: Ctx) => void) => {
this.mountedListeners.push(fn);
return this;
};
updated = (fn: (ctx: Ctx, doc: ProseNode, prevDoc: ProseNode | null) => void) => {
this.updatedListeners.push(fn);
return this;
};
markdownUpdated(fn: (ctx: Ctx, markdown: string, prevMarkdown: string | null) => void) {
this.markdownUpdatedListeners.push(fn);
return this;
}
blur(fn: (ctx: Ctx) => void) {
this.blurListeners.push(fn);
return this;
}
focus(fn: (ctx: Ctx) => void) {
this.focusListeners.push(fn);
return this;
}
destroy(fn: (ctx: Ctx) => void) {
this.destroyListeners.push(fn);
return this;
}
}
export const listenerCtx = createSlice<ListenerManager>(new ListenerManager(), 'listener');
export const listener: MilkdownPlugin = (pre) => {
pre.inject(listenerCtx);
pre.inject(listenerCtx, new ListenerManager());
return async (ctx) => {
await ctx.wait(InitReady);
const listener = ctx.get(listenerCtx);
// @ts-expect-error deprecated old listener API
if (listener.doc || listener.markdown) {
throw new Error('listener.doc and listener.markdown are deprecated, use new listener manager API instead');
}
const { listeners } = listener;
listeners.beforeMounted.forEach((fn) => fn(ctx));
await ctx.wait(SerializerReady);
const listener = ctx.get(listenerCtx);
const serializer = ctx.get(serializerCtx);
const plugin = new StatePlugin({
let prevDoc: ProseNode | null = null;
let prevMarkdown: string | null = null;
const plugin = new Plugin({
view: () => {
return {
destroy: () => {
listeners.destroy.forEach((fn) => fn(ctx));
},
};
},
props: {
handleDOMEvents: {
focus: () => {
listeners.focus.forEach((fn) => fn(ctx));
return false;
},
blur: () => {
listeners.blur.forEach((fn) => fn(ctx));
return false;
},
},
},
state: {

@@ -28,9 +119,19 @@ init: () => {

if (!tr.docChanged) return;
const { doc } = tr;
if (listeners.updated.length > 0 && (prevDoc == null || prevDoc !== doc)) {
listeners.updated.forEach((fn) => {
fn(ctx, doc, prevDoc);
});
}
if (listeners.markdownUpdated.length > 0) {
const markdown = serializer(tr.doc);
if (prevMarkdown == null || prevMarkdown !== markdown) {
listeners.markdownUpdated.forEach((fn) => {
fn(ctx, markdown, prevMarkdown);
});
prevMarkdown = markdown;
}
}
listener.markdown?.forEach((markdownListener) => {
markdownListener(() => serializer(tr.doc));
});
listener.doc?.forEach((docListener) => {
docListener(tr.doc);
});
prevDoc = doc;
},

@@ -40,3 +141,6 @@ },

ctx.update(prosePluginsCtx, (x) => x.concat(plugin));
await ctx.wait(EditorViewReady);
listeners.mounted.forEach((fn) => fn(ctx));
};
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc