@kungal/editor-core 0.17.0 → 0.18.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.
@@ -4,7 +4,7 @@ import { b as KunEditorLocale, N as Notify, U as UploadImage, K as KunEditorAdap
4
4
  import * as _milkdown_kit_utils from '@milkdown/kit/utils';
5
5
  import { Extension } from '@codemirror/state';
6
6
  import { Uploader } from '@milkdown/kit/plugin/upload';
7
- import { Decoration } from '@milkdown/kit/prose/view';
7
+ import { Decoration, EditorView } from '@milkdown/kit/prose/view';
8
8
  import { HighlightStyle } from '@codemirror/language';
9
9
 
10
10
  /** DOM attrs for the rendered spoiler chip. The styling references KunUI CSS
@@ -174,7 +174,8 @@ interface UploadPluginOptions {
174
174
  * the whole batch — more robust than the forum's all-or-nothing Promise.all.
175
175
  */
176
176
  declare const createUploader: (uploadImage: UploadImage, options?: UploadPluginOptions) => Uploader;
177
- /** The in-flight "uploading…" placeholder shown at the drop position. */
177
+ /** The in-flight "uploading…" placeholder shown at the drop position (paste/drop
178
+ * path — Milkdown's `upload` plugin calls this). */
178
179
  declare const createUploadWidgetFactory: (options?: UploadPluginOptions) => (pos: number, spec: Parameters<typeof Decoration.widget>[2]) => Decoration;
179
180
  /** Applies the uploader + placeholder to a Milkdown ctx. Call after the `upload`
180
181
  * plugin has registered its config slice. */
@@ -186,6 +187,19 @@ declare const applyUploadConfig: (ctx: Ctx, uploadImage: UploadImage, options?:
186
187
  * image-free editor (galgame 简介) is expressed.
187
188
  */
188
189
  declare const createUploadPlugin: (uploadImage: UploadImage, options?: UploadPluginOptions) => MilkdownPlugin[];
190
+ /** The placeholder DecorationSet plugin — add it when uploads are enabled. */
191
+ declare const imageUploadPlaceholder: MilkdownPlugin;
192
+ /**
193
+ * Upload a File and show an in-document "uploading…" placeholder at the caret
194
+ * until it resolves, then replace it with the image (or remove it + notify on
195
+ * failure). The placeholder isn't an undo step (`addToHistory: false`), so undo
196
+ * removes the inserted image cleanly. Used by the toolbar image button.
197
+ */
198
+ declare const startImageUpload: (view: EditorView, file: File, options: {
199
+ uploadImage: UploadImage;
200
+ notify?: Notify;
201
+ locale?: KunEditorLocale;
202
+ }) => Promise<void>;
189
203
 
190
204
  type PlaceholderMode = 'doc' | 'block';
191
205
  interface PlaceholderConfig {
@@ -226,4 +240,4 @@ interface KunEditorPluginOptions {
226
240
  */
227
241
  declare const createKunEditorPlugins: (adapters?: KunEditorAdapters, features?: KunEditorFeatures, options?: KunEditorPluginOptions) => MilkdownPlugin[];
228
242
 
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 };
243
+ 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, imageUploadPlaceholder, 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, startImageUpload, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
@@ -13,7 +13,7 @@ import { visit } from 'unist-util-visit';
13
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, Plugin, PluginKey } from '@milkdown/kit/prose/state';
16
+ import { NodeSelection, TextSelection, PluginKey, Plugin } 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, DecorationSet } from '@milkdown/kit/prose/view';
27
+ import { DecorationSet, Decoration } from '@milkdown/kit/prose/view';
28
28
 
29
29
  var spoilerAttr = $nodeAttr("kun-spoiler", () => ({
30
30
  container: {
@@ -945,12 +945,15 @@ var createUploader = (uploadImage, options = {}) => {
945
945
  return nodes.filter((node) => node !== null);
946
946
  };
947
947
  };
948
+ var uploadingWidget = (locale) => {
949
+ const el = document.createElement("span");
950
+ el.className = "kun-editor__uploading";
951
+ el.textContent = uploadingLabel(locale);
952
+ return el;
953
+ };
948
954
  var createUploadWidgetFactory = (options = {}) => {
949
955
  return (pos, spec) => {
950
- const widgetDOM = document.createElement("span");
951
- widgetDOM.textContent = uploadingLabel(options.locale);
952
- widgetDOM.style.color = "var(--color-primary)";
953
- return Decoration.widget(pos, widgetDOM, spec);
956
+ return Decoration.widget(pos, uploadingWidget(options.locale), spec);
954
957
  };
955
958
  };
956
959
  var applyUploadConfig = (ctx, uploadImage, options = {}) => {
@@ -964,6 +967,76 @@ var uploadConfigPlugin = (uploadImage, options) => (ctx) => () => {
964
967
  applyUploadConfig(ctx, uploadImage, options);
965
968
  };
966
969
  var createUploadPlugin = (uploadImage, options = {}) => [...upload, uploadConfigPlugin(uploadImage, options)];
970
+ var placeholderKey = new PluginKey("KUN_IMAGE_UPLOAD_PLACEHOLDER");
971
+ var imageUploadPlaceholder = $prose(
972
+ () => new Plugin({
973
+ key: placeholderKey,
974
+ state: {
975
+ init: () => DecorationSet.empty,
976
+ apply(tr, set) {
977
+ let next = set.map(tr.mapping, tr.doc);
978
+ const meta = tr.getMeta(placeholderKey);
979
+ if (meta?.add) {
980
+ const deco = Decoration.widget(
981
+ meta.add.pos,
982
+ uploadingWidget(meta.add.locale),
983
+ { id: meta.add.id }
984
+ );
985
+ next = next.add(tr.doc, [deco]);
986
+ } else if (meta?.remove) {
987
+ const { id } = meta.remove;
988
+ next = next.remove(
989
+ next.find(void 0, void 0, (spec) => spec.id === id)
990
+ );
991
+ }
992
+ return next;
993
+ }
994
+ },
995
+ props: {
996
+ decorations: (state) => placeholderKey.getState(state)
997
+ }
998
+ })
999
+ );
1000
+ var findPlaceholder = (view, id) => {
1001
+ const set = placeholderKey.getState(view.state);
1002
+ const found = set?.find(void 0, void 0, (spec) => spec.id === id);
1003
+ return found && found.length ? found[0].from : null;
1004
+ };
1005
+ var startImageUpload = async (view, file, options) => {
1006
+ if (!file.type.startsWith("image/")) {
1007
+ return;
1008
+ }
1009
+ const id = {};
1010
+ const tr = view.state.tr;
1011
+ if (!tr.selection.empty) {
1012
+ tr.deleteSelection();
1013
+ }
1014
+ tr.setMeta(placeholderKey, {
1015
+ add: { id, pos: tr.selection.from, locale: options.locale }
1016
+ });
1017
+ tr.setMeta("addToHistory", false);
1018
+ view.dispatch(tr);
1019
+ try {
1020
+ const src = await options.uploadImage(file);
1021
+ const pos = findPlaceholder(view, id);
1022
+ if (pos === null) {
1023
+ return;
1024
+ }
1025
+ const imageType = view.state.schema.nodes.image;
1026
+ const node = imageType?.createAndFill({ src, alt: file.name });
1027
+ if (!node) {
1028
+ return;
1029
+ }
1030
+ view.dispatch(
1031
+ view.state.tr.replaceWith(pos, pos, node).setMeta(placeholderKey, { remove: { id } })
1032
+ );
1033
+ } catch {
1034
+ view.dispatch(
1035
+ view.state.tr.setMeta(placeholderKey, { remove: { id } }).setMeta("addToHistory", false)
1036
+ );
1037
+ options.notify?.(uploadFailedLabel(options.locale), "error");
1038
+ }
1039
+ };
967
1040
  var SKIP_PARENTS = /* @__PURE__ */ new Set([
968
1041
  "code_block",
969
1042
  "bullet_list",
@@ -1071,6 +1144,7 @@ var createKunEditorPlugins = (adapters = {}, features = {}, options = {}) => {
1071
1144
  notify: adapters.notify
1072
1145
  })
1073
1146
  );
1147
+ plugins.push(imageUploadPlaceholder);
1074
1148
  }
1075
1149
  plugins.push(createStopLinkPlugin());
1076
1150
  plugins.push(createHeadingPlugin());
@@ -1085,6 +1159,6 @@ var createKunEditorPlugins = (adapters = {}, features = {}, options = {}) => {
1085
1159
  return plugins.flat();
1086
1160
  };
1087
1161
 
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 };
1162
+ export { applyCodeBlockConfig, applyUploadConfig, blockKatexSchema, chevronDownIcon, clearIcon, copyIcon, createCodeBlockPlugins, createHeadingPlugin, createKatexPlugins, createKunEditorPlugins, createMentionPlugin, createPlaceholderPlugin, createQuotePlugin, createSpoilerPlugin, createStopLinkPlugin, createUploadPlugin, createUploadWidgetFactory, createUploader, editIcon, imageUploadPlaceholder, 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, startImageUpload, stopLinkCommand, toggleLatexCommand, visibilityOffIcon };
1089
1163
  //# sourceMappingURL=index.js.map
1090
1164
  //# sourceMappingURL=index.js.map