@portabletext/editor 3.2.6 → 3.3.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.
package/lib/index.js CHANGED
@@ -19,6 +19,7 @@ import { setup, fromCallback, assign, and, enqueueActions, emit, assertEvent, ra
19
19
  import { compileSchemaDefinitionToPortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, portableTextMemberSchemaTypesToSchema } from "@portabletext/sanity-bridge";
20
20
  import { htmlToBlocks } from "@portabletext/block-tools";
21
21
  import { toHTML } from "@portabletext/to-html";
22
+ import { markdownToPortableText, portableTextToMarkdown } from "@portabletext/markdown";
22
23
  import { Schema } from "@sanity/schema";
23
24
  import flatten from "lodash/flatten.js";
24
25
  import { set, applyAll, unset, insert, setIfMissing, diffMatchPatch as diffMatchPatch$1 } from "@portabletext/patches";
@@ -2733,6 +2734,57 @@ function createConverterTextHtml(legacySchema) {
2733
2734
  }
2734
2735
  };
2735
2736
  }
2737
+ const converterTextMarkdown = {
2738
+ mimeType: "text/markdown",
2739
+ serialize: ({
2740
+ snapshot,
2741
+ event
2742
+ }) => {
2743
+ if (!snapshot.context.selection)
2744
+ return {
2745
+ type: "serialization.failure",
2746
+ mimeType: "text/markdown",
2747
+ reason: "No selection",
2748
+ originEvent: event.originEvent
2749
+ };
2750
+ const blocks = getSelectedValue(snapshot);
2751
+ return {
2752
+ type: "serialization.success",
2753
+ data: portableTextToMarkdown(blocks),
2754
+ mimeType: "text/markdown",
2755
+ originEvent: event.originEvent
2756
+ };
2757
+ },
2758
+ deserialize: ({
2759
+ snapshot,
2760
+ event
2761
+ }) => {
2762
+ const parsedBlocks = markdownToPortableText(event.data, {
2763
+ keyGenerator: snapshot.context.keyGenerator,
2764
+ schema: snapshot.context.schema
2765
+ }).flatMap((block) => {
2766
+ const parsedBlock = parseBlock({
2767
+ context: snapshot.context,
2768
+ block,
2769
+ options: {
2770
+ normalize: !1,
2771
+ removeUnusedMarkDefs: !0,
2772
+ validateFields: !1
2773
+ }
2774
+ });
2775
+ return parsedBlock ? [parsedBlock] : [];
2776
+ });
2777
+ return parsedBlocks.length === 0 ? {
2778
+ type: "deserialization.failure",
2779
+ mimeType: "text/markdown",
2780
+ reason: "No blocks deserialized"
2781
+ } : {
2782
+ type: "deserialization.success",
2783
+ data: parsedBlocks,
2784
+ mimeType: "text/markdown"
2785
+ };
2786
+ }
2787
+ };
2736
2788
  function createConverterTextPlain(legacySchema) {
2737
2789
  return {
2738
2790
  mimeType: "text/plain",
@@ -2796,7 +2848,7 @@ function escapeHtml(str) {
2796
2848
  return String(str).replace(/[&<>"'`=/]/g, (s) => entityMap[s]);
2797
2849
  }
2798
2850
  function createCoreConverters(legacySchema) {
2799
- return [converterJson, converterPortableText, createConverterTextHtml(legacySchema), createConverterTextPlain(legacySchema)];
2851
+ return [converterJson, converterPortableText, converterTextMarkdown, createConverterTextHtml(legacySchema), createConverterTextPlain(legacySchema)];
2800
2852
  }
2801
2853
  function compileType(rawType) {
2802
2854
  return Schema.compile({
@@ -8606,41 +8658,37 @@ const abstractAnnotationBehaviors = [defineBehavior({
8606
8658
  ...event,
8607
8659
  type: "delete"
8608
8660
  })]]
8609
- })], abstractDeserializeBehaviors = [
8661
+ })], mimeTypePriority = ["application/x-portable-text", "application/json", "text/markdown", "text/html", "text/plain"];
8662
+ function getFirstAvailableData({
8663
+ dataTransfer,
8664
+ startAfter
8665
+ }) {
8666
+ const startIndex = startAfter ? mimeTypePriority.indexOf(startAfter) + 1 : 0;
8667
+ for (let index = startIndex; index < mimeTypePriority.length; index++) {
8668
+ const mimeType = mimeTypePriority.at(index);
8669
+ if (!mimeType)
8670
+ continue;
8671
+ const data = dataTransfer.getData(mimeType);
8672
+ if (data)
8673
+ return {
8674
+ mimeType,
8675
+ data
8676
+ };
8677
+ }
8678
+ }
8679
+ const abstractDeserializeBehaviors = [
8610
8680
  defineBehavior({
8611
8681
  on: "deserialize",
8612
8682
  guard: ({
8613
8683
  event
8614
8684
  }) => {
8615
- const portableText = event.originEvent.originEvent.dataTransfer.getData("application/x-portable-text");
8616
- if (portableText)
8617
- return {
8618
- type: "deserialize.data",
8619
- mimeType: "application/x-portable-text",
8620
- data: portableText,
8621
- originEvent: event.originEvent
8622
- };
8623
- const json = event.originEvent.originEvent.dataTransfer.getData("application/json");
8624
- if (json)
8625
- return {
8626
- type: "deserialize.data",
8627
- mimeType: "application/json",
8628
- data: json,
8629
- originEvent: event.originEvent
8630
- };
8631
- const html = event.originEvent.originEvent.dataTransfer.getData("text/html");
8632
- if (html)
8633
- return {
8634
- type: "deserialize.data",
8635
- mimeType: "text/html",
8636
- data: html,
8637
- originEvent: event.originEvent
8638
- };
8639
- const text = event.originEvent.originEvent.dataTransfer.getData("text/plain");
8640
- return text ? {
8685
+ const availableData = getFirstAvailableData({
8686
+ dataTransfer: event.originEvent.originEvent.dataTransfer
8687
+ });
8688
+ return availableData ? {
8641
8689
  type: "deserialize.data",
8642
- mimeType: "text/plain",
8643
- data: text,
8690
+ mimeType: availableData.mimeType,
8691
+ data: availableData.data,
8644
8692
  originEvent: event.originEvent
8645
8693
  } : !1;
8646
8694
  },
@@ -8747,37 +8795,18 @@ const abstractAnnotationBehaviors = [defineBehavior({
8747
8795
  guard: ({
8748
8796
  event
8749
8797
  }) => {
8750
- if (event.mimeType === "application/x-portable-text") {
8751
- const json = event.originEvent.originEvent.dataTransfer.getData("application/json");
8752
- if (json)
8753
- return {
8754
- type: "deserialize.data",
8755
- mimeType: "application/json",
8756
- data: json,
8757
- originEvent: event.originEvent
8758
- };
8759
- }
8760
- if (event.mimeType === "application/json") {
8761
- const html = event.originEvent.originEvent.dataTransfer.getData("text/html");
8762
- if (html)
8763
- return {
8764
- type: "deserialize.data",
8765
- mimeType: "text/html",
8766
- data: html,
8767
- originEvent: event.originEvent
8768
- };
8769
- }
8770
- if (event.mimeType === "text/html") {
8771
- const text = event.originEvent.originEvent.dataTransfer.getData("text/plain");
8772
- if (text)
8773
- return {
8774
- type: "deserialize.data",
8775
- mimeType: "text/plain",
8776
- data: text,
8777
- originEvent: event.originEvent
8778
- };
8779
- }
8780
- return !1;
8798
+ if (event.mimeType === "*/*")
8799
+ return !1;
8800
+ const availableData = getFirstAvailableData({
8801
+ dataTransfer: event.originEvent.originEvent.dataTransfer,
8802
+ startAfter: event.mimeType
8803
+ });
8804
+ return availableData ? {
8805
+ type: "deserialize.data",
8806
+ mimeType: availableData.mimeType,
8807
+ data: availableData.data,
8808
+ originEvent: event.originEvent
8809
+ } : !1;
8781
8810
  },
8782
8811
  actions: [(_, deserializeDataEvent) => [raise(deserializeDataEvent)]]
8783
8812
  }),