@kungal/editor-core 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -187,12 +187,29 @@ declare const applyUploadConfig: (ctx: Ctx, uploadImage: UploadImage, options?:
187
187
  */
188
188
  declare const createUploadPlugin: (uploadImage: UploadImage, options?: UploadPluginOptions) => MilkdownPlugin[];
189
189
 
190
+ type PlaceholderMode = 'doc' | 'block';
191
+ interface PlaceholderConfig {
192
+ /** The placeholder text. Empty string disables the placeholder. */
193
+ text?: string;
194
+ /** `'block'` (default) shows on any empty block; `'doc'` only when empty. */
195
+ mode?: PlaceholderMode;
196
+ }
197
+ /**
198
+ * The placeholder plugin. `text` is baked in at creation (the editor is built
199
+ * once); pass it from `createKunEditorPlugins(..., { placeholder })`.
200
+ */
201
+ declare const createPlaceholderPlugin: (config?: PlaceholderConfig) => MilkdownPlugin[];
202
+
190
203
  /** Extra, non-adapter options for the composed bundle. */
191
204
  interface KunEditorPluginOptions {
192
205
  /** UI language for plugin chrome (code-block toolbar labels). Default `'zh-cn'`. */
193
206
  locale?: KunEditorLocale;
194
207
  /** KaTeX options forwarded to the code-block `latex` preview. */
195
208
  katexOptions?: KatexOptions;
209
+ /** Placeholder text shown when empty. Omit / empty string → no placeholder. */
210
+ placeholder?: string;
211
+ /** `'block'` (default) shows on any empty block; `'doc'` only when empty. */
212
+ placeholderMode?: 'doc' | 'block';
196
213
  }
197
214
  /**
198
215
  * Assemble the KunEditor Milkdown plugin list.
@@ -209,4 +226,4 @@ interface KunEditorPluginOptions {
209
226
  */
210
227
  declare const createKunEditorPlugins: (adapters?: KunEditorAdapters, features?: KunEditorFeatures, options?: KunEditorPluginOptions) => MilkdownPlugin[];
211
228
 
212
- export { type CodeBlockOptions, type KunEditorPluginOptions, type MentionUrlConfig, type QuoteReference, type UploadPluginOptions, applyCodeBlockConfig, applyUploadConfig, blockKatexSchema, chevronDownIcon, clearIcon, copyIcon, createCodeBlockPlugins, createHeadingPlugin, createKatexPlugins, createKunEditorPlugins, createMentionPlugin, createQuotePlugin, createSpoilerPlugin, createStopLinkPlugin, createUploadPlugin, createUploadWidgetFactory, createUploader, editIcon, insertKunSpoilerCommand, insertMentionCommand, insertQuoteCommand, insertSpoilerInputRule, kunCM, kunCMHighlightStyle, kunCMTheme, linkCustomKeymap, mathBlockInputRule, mathInlineId, mathInlineInputRule, mathInlineSchema, mentionId, mentionSchema, quoteId, quoteSchema, remarkMathBlockPlugin, remarkMathPlugin, remarkMentionPlugin, remarkQuotePlugin, remarkSpoilerPlugin, searchIcon, setHeadingCommand, spoilerAttr, spoilerSchema, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
229
+ export { type CodeBlockOptions, type KunEditorPluginOptions, type MentionUrlConfig, type PlaceholderConfig, type PlaceholderMode, type QuoteReference, type UploadPluginOptions, applyCodeBlockConfig, applyUploadConfig, blockKatexSchema, chevronDownIcon, clearIcon, copyIcon, createCodeBlockPlugins, createHeadingPlugin, createKatexPlugins, createKunEditorPlugins, createMentionPlugin, createPlaceholderPlugin, createQuotePlugin, createSpoilerPlugin, createStopLinkPlugin, createUploadPlugin, createUploadWidgetFactory, createUploader, editIcon, insertKunSpoilerCommand, insertMentionCommand, insertQuoteCommand, insertSpoilerInputRule, kunCM, kunCMHighlightStyle, kunCMTheme, linkCustomKeymap, mathBlockInputRule, mathInlineId, mathInlineInputRule, mathInlineSchema, mentionId, mentionSchema, quoteId, quoteSchema, remarkMathBlockPlugin, remarkMathPlugin, remarkMentionPlugin, remarkQuotePlugin, remarkSpoilerPlugin, searchIcon, setHeadingCommand, spoilerAttr, spoilerSchema, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
@@ -187,12 +187,29 @@ declare const applyUploadConfig: (ctx: Ctx, uploadImage: UploadImage, options?:
187
187
  */
188
188
  declare const createUploadPlugin: (uploadImage: UploadImage, options?: UploadPluginOptions) => MilkdownPlugin[];
189
189
 
190
+ type PlaceholderMode = 'doc' | 'block';
191
+ interface PlaceholderConfig {
192
+ /** The placeholder text. Empty string disables the placeholder. */
193
+ text?: string;
194
+ /** `'block'` (default) shows on any empty block; `'doc'` only when empty. */
195
+ mode?: PlaceholderMode;
196
+ }
197
+ /**
198
+ * The placeholder plugin. `text` is baked in at creation (the editor is built
199
+ * once); pass it from `createKunEditorPlugins(..., { placeholder })`.
200
+ */
201
+ declare const createPlaceholderPlugin: (config?: PlaceholderConfig) => MilkdownPlugin[];
202
+
190
203
  /** Extra, non-adapter options for the composed bundle. */
191
204
  interface KunEditorPluginOptions {
192
205
  /** UI language for plugin chrome (code-block toolbar labels). Default `'zh-cn'`. */
193
206
  locale?: KunEditorLocale;
194
207
  /** KaTeX options forwarded to the code-block `latex` preview. */
195
208
  katexOptions?: KatexOptions;
209
+ /** Placeholder text shown when empty. Omit / empty string → no placeholder. */
210
+ placeholder?: string;
211
+ /** `'block'` (default) shows on any empty block; `'doc'` only when empty. */
212
+ placeholderMode?: 'doc' | 'block';
196
213
  }
197
214
  /**
198
215
  * Assemble the KunEditor Milkdown plugin list.
@@ -209,4 +226,4 @@ interface KunEditorPluginOptions {
209
226
  */
210
227
  declare const createKunEditorPlugins: (adapters?: KunEditorAdapters, features?: KunEditorFeatures, options?: KunEditorPluginOptions) => MilkdownPlugin[];
211
228
 
212
- export { type CodeBlockOptions, type KunEditorPluginOptions, type MentionUrlConfig, type QuoteReference, type UploadPluginOptions, applyCodeBlockConfig, applyUploadConfig, blockKatexSchema, chevronDownIcon, clearIcon, copyIcon, createCodeBlockPlugins, createHeadingPlugin, createKatexPlugins, createKunEditorPlugins, createMentionPlugin, createQuotePlugin, createSpoilerPlugin, createStopLinkPlugin, createUploadPlugin, createUploadWidgetFactory, createUploader, editIcon, insertKunSpoilerCommand, insertMentionCommand, insertQuoteCommand, insertSpoilerInputRule, kunCM, kunCMHighlightStyle, kunCMTheme, linkCustomKeymap, mathBlockInputRule, mathInlineId, mathInlineInputRule, mathInlineSchema, mentionId, mentionSchema, quoteId, quoteSchema, remarkMathBlockPlugin, remarkMathPlugin, remarkMentionPlugin, remarkQuotePlugin, remarkSpoilerPlugin, searchIcon, setHeadingCommand, spoilerAttr, spoilerSchema, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
229
+ export { type CodeBlockOptions, type KunEditorPluginOptions, type MentionUrlConfig, type PlaceholderConfig, type PlaceholderMode, type QuoteReference, type UploadPluginOptions, applyCodeBlockConfig, applyUploadConfig, blockKatexSchema, chevronDownIcon, clearIcon, copyIcon, createCodeBlockPlugins, createHeadingPlugin, createKatexPlugins, createKunEditorPlugins, createMentionPlugin, createPlaceholderPlugin, createQuotePlugin, createSpoilerPlugin, createStopLinkPlugin, createUploadPlugin, createUploadWidgetFactory, createUploader, editIcon, insertKunSpoilerCommand, insertMentionCommand, insertQuoteCommand, insertSpoilerInputRule, kunCM, kunCMHighlightStyle, kunCMTheme, linkCustomKeymap, mathBlockInputRule, mathInlineId, mathInlineInputRule, mathInlineSchema, mentionId, mentionSchema, quoteId, quoteSchema, remarkMathBlockPlugin, remarkMathPlugin, remarkMentionPlugin, remarkQuotePlugin, remarkSpoilerPlugin, searchIcon, setHeadingCommand, spoilerAttr, spoilerSchema, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
@@ -8,12 +8,12 @@ import { indent } from '@milkdown/kit/plugin/indent';
8
8
  import { trailing } from '@milkdown/kit/plugin/trailing';
9
9
  import { expectDomTypeError } from '@milkdown/kit/exception';
10
10
  import { InputRule, textblockTypeInputRule } from '@milkdown/kit/prose/inputrules';
11
- import { $nodeAttr, $nodeSchema, $command, $inputRule, $remark, $useKeymap } from '@milkdown/kit/utils';
11
+ import { $nodeAttr, $nodeSchema, $command, $inputRule, $remark, $useKeymap, $prose } from '@milkdown/kit/utils';
12
12
  import { visit } from 'unist-util-visit';
13
- import { commandsCtx } from '@milkdown/kit/core';
13
+ import { commandsCtx, editorViewOptionsCtx } from '@milkdown/kit/core';
14
14
  import { setBlockType } from '@milkdown/kit/prose/commands';
15
15
  import { findNodeInSelection, nodeRule } from '@milkdown/kit/prose';
16
- import { NodeSelection, TextSelection } from '@milkdown/kit/prose/state';
16
+ import { NodeSelection, TextSelection, Plugin, PluginKey } from '@milkdown/kit/prose/state';
17
17
  import katex from 'katex';
18
18
  import remarkMath from 'remark-math';
19
19
  import { codeBlockComponent, codeBlockConfig } from '@milkdown/kit/component/code-block';
@@ -24,7 +24,7 @@ import { basicSetup } from 'codemirror';
24
24
  import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';
25
25
  import { tags } from '@lezer/highlight';
26
26
  import { upload, uploadConfig } from '@milkdown/kit/plugin/upload';
27
- import { Decoration } from '@milkdown/kit/prose/view';
27
+ import { Decoration, DecorationSet } from '@milkdown/kit/prose/view';
28
28
 
29
29
  var spoilerAttr = $nodeAttr("kun-spoiler", () => ({
30
30
  container: {
@@ -964,6 +964,62 @@ var uploadConfigPlugin = (uploadImage, options) => (ctx) => () => {
964
964
  applyUploadConfig(ctx, uploadImage, options);
965
965
  };
966
966
  var createUploadPlugin = (uploadImage, options = {}) => [...upload, uploadConfigPlugin(uploadImage, options)];
967
+ var SKIP_PARENTS = /* @__PURE__ */ new Set([
968
+ "code_block",
969
+ "bullet_list",
970
+ "ordered_list",
971
+ "list_item",
972
+ "table"
973
+ ]);
974
+ var isDocEmpty = (doc) => doc.childCount <= 1 && !doc.firstChild?.content.size;
975
+ var isInSkippedParent = (state) => {
976
+ const { $anchor } = state.selection;
977
+ for (let depth = $anchor.depth; depth > 0; depth--) {
978
+ if (SKIP_PARENTS.has($anchor.node(depth).type.name)) {
979
+ return true;
980
+ }
981
+ }
982
+ return false;
983
+ };
984
+ var createPlaceholderPlugin = (config = {}) => {
985
+ const text = config.text ?? "";
986
+ const mode = config.mode ?? "block";
987
+ const plugin = $prose(
988
+ (ctx) => new Plugin({
989
+ key: new PluginKey("KUN_PLACEHOLDER"),
990
+ props: {
991
+ decorations: (state) => {
992
+ if (!text) {
993
+ return null;
994
+ }
995
+ const editable = ctx.get(editorViewOptionsCtx).editable;
996
+ if (editable && !editable(state)) {
997
+ return null;
998
+ }
999
+ if (mode === "doc" && !isDocEmpty(state.doc)) {
1000
+ return null;
1001
+ }
1002
+ const { selection } = state;
1003
+ if (!selection.empty || isInSkippedParent(state)) {
1004
+ return null;
1005
+ }
1006
+ const node = selection.$anchor.parent;
1007
+ if (node.content.size > 0) {
1008
+ return null;
1009
+ }
1010
+ const before = selection.$anchor.before();
1011
+ return DecorationSet.create(state.doc, [
1012
+ Decoration.node(before, before + node.nodeSize, {
1013
+ class: "kun-editor__placeholder",
1014
+ "data-placeholder": text
1015
+ })
1016
+ ]);
1017
+ }
1018
+ }
1019
+ })
1020
+ );
1021
+ return [plugin].flat();
1022
+ };
967
1023
 
968
1024
  // src/preset/index.ts
969
1025
  var createKunEditorPlugins = (adapters = {}, features = {}, options = {}) => {
@@ -1018,9 +1074,17 @@ var createKunEditorPlugins = (adapters = {}, features = {}, options = {}) => {
1018
1074
  }
1019
1075
  plugins.push(createStopLinkPlugin());
1020
1076
  plugins.push(createHeadingPlugin());
1077
+ if (options.placeholder) {
1078
+ plugins.push(
1079
+ createPlaceholderPlugin({
1080
+ text: options.placeholder,
1081
+ mode: options.placeholderMode
1082
+ })
1083
+ );
1084
+ }
1021
1085
  return plugins.flat();
1022
1086
  };
1023
1087
 
1024
- export { applyCodeBlockConfig, applyUploadConfig, blockKatexSchema, chevronDownIcon, clearIcon, copyIcon, createCodeBlockPlugins, createHeadingPlugin, createKatexPlugins, createKunEditorPlugins, createMentionPlugin, createQuotePlugin, createSpoilerPlugin, createStopLinkPlugin, createUploadPlugin, createUploadWidgetFactory, createUploader, editIcon, insertKunSpoilerCommand, insertMentionCommand, insertQuoteCommand, insertSpoilerInputRule, kunCM, kunCMHighlightStyle, kunCMTheme, linkCustomKeymap, mathBlockInputRule, mathInlineId, mathInlineInputRule, mathInlineSchema, mentionId, mentionSchema, quoteId, quoteSchema, remarkMathBlockPlugin, remarkMathPlugin, remarkMentionPlugin, remarkQuotePlugin, remarkSpoilerPlugin, searchIcon, setHeadingCommand, spoilerAttr, spoilerSchema, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
1088
+ export { applyCodeBlockConfig, applyUploadConfig, blockKatexSchema, chevronDownIcon, clearIcon, copyIcon, createCodeBlockPlugins, createHeadingPlugin, createKatexPlugins, createKunEditorPlugins, createMentionPlugin, createPlaceholderPlugin, createQuotePlugin, createSpoilerPlugin, createStopLinkPlugin, createUploadPlugin, createUploadWidgetFactory, createUploader, editIcon, insertKunSpoilerCommand, insertMentionCommand, insertQuoteCommand, insertSpoilerInputRule, kunCM, kunCMHighlightStyle, kunCMTheme, linkCustomKeymap, mathBlockInputRule, mathInlineId, mathInlineInputRule, mathInlineSchema, mentionId, mentionSchema, quoteId, quoteSchema, remarkMathBlockPlugin, remarkMathPlugin, remarkMentionPlugin, remarkQuotePlugin, remarkSpoilerPlugin, searchIcon, setHeadingCommand, spoilerAttr, spoilerSchema, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
1025
1089
  //# sourceMappingURL=index.js.map
1026
1090
  //# sourceMappingURL=index.js.map