@symbo.ls/sdk 2.31.36 → 2.32.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.
@@ -704,6 +704,279 @@ var BaseService = class {
704
704
  }
705
705
  };
706
706
 
707
+ // src/utils/ordering.js
708
+ function isObjectLike(val) {
709
+ return val && typeof val === "object" && !Array.isArray(val);
710
+ }
711
+ function normalizePath(path) {
712
+ if (Array.isArray(path)) {
713
+ return path;
714
+ }
715
+ if (typeof path === "string") {
716
+ return [path];
717
+ }
718
+ return [];
719
+ }
720
+ function getParentPathsFromTuples(tuples = []) {
721
+ const seen = /* @__PURE__ */ new Set();
722
+ const parents = [];
723
+ const META_KEYS = /* @__PURE__ */ new Set([
724
+ "style",
725
+ "class",
726
+ "text",
727
+ "html",
728
+ "content",
729
+ "data",
730
+ "attr",
731
+ "state",
732
+ "scope",
733
+ "props",
734
+ "define",
735
+ "on",
736
+ "extend",
737
+ "extends",
738
+ "childExtend",
739
+ "childExtends",
740
+ "childProps",
741
+ "children",
742
+ "component",
743
+ "context",
744
+ "tag",
745
+ "key",
746
+ "__order",
747
+ "if"
748
+ ]);
749
+ for (let i = 0; i < tuples.length; i++) {
750
+ const tuple = tuples[i];
751
+ if (!Array.isArray(tuple) || tuple.length < 2) {
752
+ continue;
753
+ }
754
+ const path = normalizePath(tuple[1]);
755
+ if (!path.length) {
756
+ continue;
757
+ }
758
+ if (path[0] === "schema") {
759
+ continue;
760
+ }
761
+ const immediateParent = path.slice(0, -1);
762
+ if (immediateParent.length) {
763
+ const key = JSON.stringify(immediateParent);
764
+ if (!seen.has(key)) {
765
+ seen.add(key);
766
+ parents.push(immediateParent);
767
+ }
768
+ }
769
+ const last = path[path.length - 1];
770
+ if (META_KEYS.has(last) && path.length >= 2) {
771
+ const containerParent = path.slice(0, -2);
772
+ if (containerParent.length) {
773
+ const key2 = JSON.stringify(containerParent);
774
+ if (!seen.has(key2)) {
775
+ seen.add(key2);
776
+ parents.push(containerParent);
777
+ }
778
+ }
779
+ }
780
+ for (let j = 0; j < path.length; j++) {
781
+ const seg = path[j];
782
+ if (!META_KEYS.has(seg)) {
783
+ continue;
784
+ }
785
+ const containerParent2 = path.slice(0, j);
786
+ if (!containerParent2.length) {
787
+ continue;
788
+ }
789
+ const key3 = JSON.stringify(containerParent2);
790
+ if (!seen.has(key3)) {
791
+ seen.add(key3);
792
+ parents.push(containerParent2);
793
+ }
794
+ }
795
+ }
796
+ return parents;
797
+ }
798
+ function computeOrdersFromState(root, parentPaths = []) {
799
+ if (!root || typeof root.getByPath !== "function") {
800
+ return [];
801
+ }
802
+ const orders = [];
803
+ const EXCLUDE_KEYS = /* @__PURE__ */ new Set(["__order"]);
804
+ for (let i = 0; i < parentPaths.length; i++) {
805
+ const parentPath = parentPaths[i];
806
+ const obj = (() => {
807
+ try {
808
+ return root.getByPath(parentPath);
809
+ } catch {
810
+ return null;
811
+ }
812
+ })();
813
+ if (!isObjectLike(obj)) {
814
+ continue;
815
+ }
816
+ const keys = Object.keys(obj).filter((k) => !EXCLUDE_KEYS.has(k));
817
+ orders.push({ path: parentPath, keys });
818
+ }
819
+ return orders;
820
+ }
821
+ function normaliseSchemaCode(code) {
822
+ if (typeof code !== "string" || !code.length) {
823
+ return "";
824
+ }
825
+ return code.replaceAll("/////n", "\n").replaceAll("/////tilde", "`");
826
+ }
827
+ function extractTopLevelKeysFromCode(code) {
828
+ const src = normaliseSchemaCode(code);
829
+ if (!src) {
830
+ return [];
831
+ }
832
+ const idx = src.indexOf("export default");
833
+ if (idx === -1) {
834
+ return [];
835
+ }
836
+ let i = src.indexOf("{", idx);
837
+ if (i === -1) {
838
+ return [];
839
+ }
840
+ const keys = [];
841
+ let depth = 0;
842
+ let inStr = false;
843
+ let strCh = "";
844
+ let inEsc = false;
845
+ for (; i < src.length; i++) {
846
+ const ch = src[i];
847
+ if (inStr) {
848
+ if (inEsc) {
849
+ inEsc = false;
850
+ } else if (ch === "\\") {
851
+ inEsc = true;
852
+ } else if (ch === strCh) {
853
+ inStr = false;
854
+ strCh = "";
855
+ }
856
+ continue;
857
+ }
858
+ if (ch === '"' || ch === "'" || ch === "`") {
859
+ inStr = true;
860
+ strCh = ch;
861
+ continue;
862
+ }
863
+ if (ch === "{") {
864
+ depth++;
865
+ continue;
866
+ }
867
+ if (ch === "}") {
868
+ depth--;
869
+ if (depth === 0) {
870
+ break;
871
+ }
872
+ continue;
873
+ }
874
+ if (depth !== 1) {
875
+ continue;
876
+ }
877
+ if (/[A-Za-z_$]/u.test(ch)) {
878
+ let j = i;
879
+ while (j < src.length && /[A-Za-z0-9_$]/u.test(src[j])) {
880
+ j++;
881
+ }
882
+ let k = j;
883
+ while (k < src.length && /\s/u.test(src[k])) {
884
+ k++;
885
+ }
886
+ if (src[k] === ":") {
887
+ const key = src.slice(i, j);
888
+ keys.push(key);
889
+ }
890
+ i = j;
891
+ continue;
892
+ }
893
+ }
894
+ if (!keys.length) {
895
+ const bodyStart = src.indexOf("{", idx);
896
+ const bodyEnd = src.lastIndexOf("}");
897
+ if (bodyStart === -1 || bodyEnd === -1 || bodyEnd <= bodyStart) {
898
+ return Array.from(new Set(keys));
899
+ }
900
+ const body = src.slice(bodyStart + 1, bodyEnd);
901
+ const re = /(?:[A-Za-z_$][A-Za-z0-9_$]*|"[^"]+"|'[^']+')\s*:/gu;
902
+ for (const m of body.matchAll(re)) {
903
+ const raw = m[0].split(":")[0].trim();
904
+ const key = raw[0] === '"' || raw[0] === "'" ? raw.slice(1, -1) : raw;
905
+ keys.push(key);
906
+ }
907
+ }
908
+ return Array.from(new Set(keys));
909
+ }
910
+ function computeOrdersForTuples(root, tuples = []) {
911
+ const preferredOrderMap = /* @__PURE__ */ new Map();
912
+ for (let i = 0; i < tuples.length; i++) {
913
+ const t = tuples[i];
914
+ if (!Array.isArray(t)) {
915
+ continue;
916
+ }
917
+ const [action, path, value] = t;
918
+ const p = normalizePath(path);
919
+ if (action !== "update" || !Array.isArray(p) || p.length < 3) {
920
+ continue;
921
+ }
922
+ if (p[0] !== "schema") {
923
+ continue;
924
+ }
925
+ const [, type, key] = p;
926
+ const containerPath = [type, key];
927
+ const uses = value && Array.isArray(value.uses) ? value.uses : null;
928
+ const code = value && value.code;
929
+ const obj = (() => {
930
+ try {
931
+ return root && typeof root.getByPath === "function" ? root.getByPath(containerPath) : null;
932
+ } catch {
933
+ return null;
934
+ }
935
+ })();
936
+ if (!obj) {
937
+ continue;
938
+ }
939
+ const present = new Set(Object.keys(obj));
940
+ const EXCLUDE_KEYS = /* @__PURE__ */ new Set(["__order"]);
941
+ const codeKeys = extractTopLevelKeysFromCode(code);
942
+ let resolved = [];
943
+ if (Array.isArray(codeKeys) && codeKeys.length) {
944
+ resolved = codeKeys.filter((k) => present.has(k) && !EXCLUDE_KEYS.has(k));
945
+ }
946
+ if (resolved.length && Array.isArray(uses) && uses.length) {
947
+ for (let u = 0; u < uses.length; u++) {
948
+ const keyName = uses[u];
949
+ if (present.has(keyName) && !EXCLUDE_KEYS.has(keyName) && !resolved.includes(keyName)) {
950
+ resolved.push(keyName);
951
+ }
952
+ }
953
+ }
954
+ if (resolved.length) {
955
+ preferredOrderMap.set(JSON.stringify(containerPath), { path: containerPath, keys: resolved });
956
+ }
957
+ }
958
+ const parents = getParentPathsFromTuples(tuples);
959
+ const orders = [];
960
+ const seen = /* @__PURE__ */ new Set();
961
+ preferredOrderMap.forEach((v) => {
962
+ const k = JSON.stringify(v.path);
963
+ if (!seen.has(k)) {
964
+ seen.add(k);
965
+ orders.push(v);
966
+ }
967
+ });
968
+ const fallbackOrders = computeOrdersFromState(root, parents);
969
+ for (let i = 0; i < fallbackOrders.length; i++) {
970
+ const v = fallbackOrders[i];
971
+ const k = JSON.stringify(v.path);
972
+ if (!seen.has(k)) {
973
+ seen.add(k);
974
+ orders.push(v);
975
+ }
976
+ }
977
+ return orders;
978
+ }
979
+
707
980
  // src/services/ProjectService.js
708
981
  var ProjectService = class extends BaseService {
709
982
  // ==================== PROJECT METHODS ====================
@@ -1236,6 +1509,8 @@ var ProjectService = class extends BaseService {
1236
1509
  throw new Error("Changes must be an array");
1237
1510
  }
1238
1511
  const { message, branch = "main", type = "patch" } = options;
1512
+ const state = this._context && this._context.state;
1513
+ const derivedOrders = options.orders || (state ? computeOrdersForTuples(state, changes) : []);
1239
1514
  try {
1240
1515
  const response = await this._request(`/projects/${projectId}/changes`, {
1241
1516
  method: "POST",
@@ -1243,7 +1518,8 @@ var ProjectService = class extends BaseService {
1243
1518
  changes,
1244
1519
  message,
1245
1520
  branch,
1246
- type
1521
+ type,
1522
+ ...derivedOrders && derivedOrders.length ? { orders: derivedOrders } : {}
1247
1523
  }),
1248
1524
  methodName: "applyProjectChanges"
1249
1525
  });