@ai-table/state 0.1.42 → 0.1.44

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.
@@ -733,7 +733,6 @@ class GroupCalculator {
733
733
  const linearRows = [];
734
734
  let lastGroupDepth = -1;
735
735
  let currentGroupRecords = [];
736
- let currentGroupIds = [];
737
736
  let currentGroupRecordIndices = []; // 记录当前分组中每个记录的原始索引
738
737
  // 开始添加一个空白行
739
738
  linearRows.push({
@@ -747,11 +746,10 @@ class GroupCalculator {
747
746
  if (groupTabRows.length > 0) {
748
747
  // 如果有新的分组标签,先处理上一个分组的结束
749
748
  if (currentGroupRecords.length > 0) {
750
- this.handleGroupEnd(currentGroupRecords, linearRows, currentGroupIds, currentGroupRecordIndices);
749
+ this.handleGroupEnd(currentGroupRecords, linearRows, currentGroupRecordIndices);
751
750
  currentGroupRecords = [];
752
751
  currentGroupRecordIndices = [];
753
752
  }
754
- currentGroupIds = groupTabRows.map((row) => row.groupId);
755
753
  const depths = groupTabRows.filter((d) => d.depth !== undefined).map((d) => d.depth);
756
754
  const minDepth = depths.length > 0 ? Math.min(...depths) : 0;
757
755
  // 如果当前分组的最小深度小于等于上一个分组的深度,说明是同级或上级分组,需添加空白行
@@ -762,9 +760,7 @@ class GroupCalculator {
762
760
  depth: minDepth
763
761
  });
764
762
  }
765
- // 只添加未被父级折叠的分组
766
- const visibleGroupTabRows = this.filterVisibleGroupTabs(groupTabRows);
767
- linearRows.push(...visibleGroupTabRows);
763
+ linearRows.push(...groupTabRows);
768
764
  lastGroupDepth = depths.length > 0 ? Math.max(...depths) : 0;
769
765
  }
770
766
  // 将记录添加到当前分组
@@ -773,7 +769,7 @@ class GroupCalculator {
773
769
  });
774
770
  // 处理最后一个分组
775
771
  if (currentGroupRecords.length > 0) {
776
- this.handleGroupEnd(currentGroupRecords, linearRows, currentGroupIds, currentGroupRecordIndices);
772
+ this.handleGroupEnd(currentGroupRecords, linearRows, currentGroupRecordIndices);
777
773
  }
778
774
  // 添加分组结束的空白行
779
775
  if (lastGroupDepth >= 0) {
@@ -785,12 +781,11 @@ class GroupCalculator {
785
781
  }
786
782
  return linearRows;
787
783
  }
788
- handleGroupEnd(currentGroupRecords, linearRows, currentGroupIds, currentGroupRecordIndices) {
789
- // 分组结束时添加该分组的记录和add行
784
+ handleGroupEnd(currentGroupRecords, linearRows, currentGroupRecordIndices) {
790
785
  let groupDisplayRowIndex = 0;
786
+ const lastLinearRow = linearRows[linearRows.length - 1];
791
787
  currentGroupRecords.forEach((record, i) => {
792
- const recordIndex = currentGroupRecordIndices?.[i] ?? 0;
793
- if (this.shouldShowRecord(recordIndex)) {
788
+ if (lastLinearRow?.type === AITableRowType.group && !lastLinearRow.isCollapsed) {
794
789
  groupDisplayRowIndex++;
795
790
  linearRows.push({
796
791
  type: AITableRowType.record,
@@ -801,7 +796,7 @@ class GroupCalculator {
801
796
  }
802
797
  });
803
798
  // 分组未折叠,为每个分组添加add新增行
804
- if (currentGroupRecords.length > 0 && this.shouldShowAddRow(currentGroupIds)) {
799
+ if (currentGroupRecords.length > 0 && lastLinearRow?.type === AITableRowType.group && !lastLinearRow.isCollapsed) {
805
800
  let startRecordIndex = 0;
806
801
  let endRecordIndex = 0;
807
802
  if (currentGroupRecordIndices) {
@@ -826,20 +821,23 @@ class GroupCalculator {
826
821
  const field = this.fieldsMap[groupField.field_id];
827
822
  if (!field)
828
823
  return;
829
- const groupValue = AITableQueries.getFieldValue(this.aiTable, [record._id, field._id]);
830
824
  const breakpointIndex = breakpoints.indexOf(recordIndex);
831
825
  const groupId = this.generateGroupId(groupField.field_id, depth, breakpointIndex);
832
- const recordRange = this.calculateGroupRecordRange(groupField.field_id, breakpointIndex, totalRecords);
833
- groupTabRows.push({
834
- type: AITableRowType.group,
835
- _id: nanoid(),
836
- depth,
837
- fieldId: groupField.field_id,
838
- groupValue,
839
- isCollapsed: this.groupCollapseState.has(groupId),
840
- range: recordRange,
841
- groupId
842
- });
826
+ const isParentCollapsed = this.isParentGroupCollapsed(depth, recordIndex);
827
+ if (!isParentCollapsed) {
828
+ const groupValue = AITableQueries.getFieldValue(this.aiTable, [record._id, field._id]);
829
+ const recordRange = this.calculateGroupRecordRange(groupField.field_id, breakpointIndex, totalRecords);
830
+ groupTabRows.push({
831
+ type: AITableRowType.group,
832
+ _id: nanoid(),
833
+ depth,
834
+ fieldId: groupField.field_id,
835
+ groupValue,
836
+ isCollapsed: this.groupCollapseState.has(groupId),
837
+ range: recordRange,
838
+ groupId
839
+ });
840
+ }
843
841
  }
844
842
  });
845
843
  return groupTabRows;
@@ -863,61 +861,27 @@ class GroupCalculator {
863
861
  // 通过字段ID、深度和断点索引确保唯一
864
862
  return `${fieldId}_${depth}_${breakpointIndex}`;
865
863
  }
866
- shouldShowRecord(recordIndex) {
867
- for (let depth = 0; depth < this.groups.length; depth++) {
868
- const groupField = this.groups[depth];
869
- const breakpoints = this.groupBreakpoints.get(groupField.field_id) || [];
870
- // 找到当前记录所属的分组断点
871
- let belongsToBreakpointIndex = -1;
872
- for (let i = breakpoints.length - 1; i >= 0; i--) {
873
- if (breakpoints[i] <= recordIndex) {
874
- belongsToBreakpointIndex = i;
864
+ // 检查所有父级分组是否被折叠
865
+ isParentGroupCollapsed(currentDepth, recordIndex) {
866
+ for (let parentDepth = 0; parentDepth < currentDepth; parentDepth++) {
867
+ const parentGroupField = this.groups[parentDepth];
868
+ const parentBreakpoints = this.groupBreakpoints.get(parentGroupField.field_id) || [];
869
+ let parentBreakpointIndex = -1;
870
+ for (let i = parentBreakpoints.length - 1; i >= 0; i--) {
871
+ if (parentBreakpoints[i] <= recordIndex) {
872
+ parentBreakpointIndex = i;
875
873
  break;
876
874
  }
877
875
  }
878
- if (belongsToBreakpointIndex >= 0) {
879
- const groupId = this.generateGroupId(groupField.field_id, depth, belongsToBreakpointIndex);
880
- if (this.groupCollapseState.has(groupId)) {
881
- return false; // 父级分组被折叠,不显示记录
882
- }
883
- }
884
- }
885
- return true;
886
- }
887
- // 检查当前分组是否应该显示添加行
888
- shouldShowAddRow(currentGroupIds) {
889
- if (!currentGroupIds || currentGroupIds.length === 0) {
890
- return true; // 默认显示
891
- }
892
- // 检查当前组的所有分组层级是否都展开
893
- for (const groupId of currentGroupIds) {
894
- if (this.groupCollapseState.has(groupId)) {
895
- return false; // 有层级被折叠,不显示添加行
896
- }
897
- }
898
- return true;
899
- }
900
- // 过滤可见的分组标签
901
- filterVisibleGroupTabs(groupTabRows) {
902
- const visibleRows = [];
903
- for (let i = 0; i < groupTabRows.length; i++) {
904
- const currentRow = groupTabRows[i];
905
- let show = true;
906
- // 检查当前分组标签的所有父级是否都展开
907
- const currentDepth = currentRow.depth ?? 0;
908
- for (let parentDepth = 0; parentDepth < currentDepth; parentDepth++) {
909
- // 找到同一记录索引下的父级分组ID
910
- const parentRow = groupTabRows.find((row) => row.depth === parentDepth);
911
- if (parentRow && this.groupCollapseState.has(parentRow.groupId)) {
912
- show = false;
913
- break;
876
+ if (parentBreakpointIndex > -1) {
877
+ const parentGroupId = this.generateGroupId(parentGroupField.field_id, parentDepth, parentBreakpointIndex);
878
+ const isParentCollapsed = this.groupCollapseState.has(parentGroupId);
879
+ if (isParentCollapsed) {
880
+ return true;
914
881
  }
915
882
  }
916
- if (show) {
917
- visibleRows.push(currentRow);
918
- }
919
883
  }
920
- return visibleRows;
884
+ return false;
921
885
  }
922
886
  }
923
887
 
@@ -2013,6 +1977,13 @@ function getNewRecordsPosition(aiTable, options) {
2013
1977
  });
2014
1978
  return viewPositions;
2015
1979
  }
1980
+ function getParentGroupValuesByGroupId(aiTable, groupId) {
1981
+ const parentGroups = getParentLinearRowGroups(aiTable, groupId);
1982
+ return parentGroups.reduce((pre, cur) => {
1983
+ pre[cur.fieldId] = cur.groupValue;
1984
+ return pre;
1985
+ }, {});
1986
+ }
2016
1987
 
2017
1988
  function moveRecords(aiTable, options, updatedInfo) {
2018
1989
  const activeViewId = aiTable.activeViewId();
@@ -2028,7 +1999,15 @@ function moveRecords(aiTable, options, updatedInfo) {
2028
1999
  }
2029
2000
  sourceRecords.push(originalRecords[index]);
2030
2001
  });
2031
- let { targetPosition, prevPosition } = getPositionByAfterOrBeforeRecordId(aiTable, { afterRecordId, beforeRecordId });
2002
+ let { targetPosition, prevPosition } = getPositionByAfterOrBeforeRecordId(aiTable, {
2003
+ afterRecordId,
2004
+ beforeRecordId
2005
+ });
2006
+ const groups = activeView.settings?.groups;
2007
+ let needCopyGroupValuesMap = null;
2008
+ if (groups?.length && (afterRecordId || beforeRecordId)) {
2009
+ needCopyGroupValuesMap = getParentGroupValuesByGroupId(aiTable, (afterRecordId || beforeRecordId));
2010
+ }
2032
2011
  // 勾选多行顺序可能不一致,需要排序
2033
2012
  const sortedSourceRecords = sortByViewPosition(sourceRecords, activeView);
2034
2013
  let nextPosition = (prevPosition + targetPosition) / 2;
@@ -2037,6 +2016,16 @@ function moveRecords(aiTable, options, updatedInfo) {
2037
2016
  if (sourceIndex === undefined) {
2038
2017
  throw new Error(`Record with id ${record._id} not found`);
2039
2018
  }
2019
+ if (groups?.length && needCopyGroupValuesMap) {
2020
+ const updateFieldValues = [];
2021
+ groups.forEach((group) => {
2022
+ updateFieldValues.push({
2023
+ path: [record._id, group.field_id],
2024
+ value: needCopyGroupValuesMap[group.field_id]
2025
+ });
2026
+ });
2027
+ Actions.updateFieldValues(aiTable, updateFieldValues);
2028
+ }
2040
2029
  Actions.setRecordPositions(aiTable, { [activeViewId]: nextPosition }, [sourceIndex]);
2041
2030
  prevPosition = nextPosition;
2042
2031
  nextPosition = (prevPosition + targetPosition) / 2;
@@ -2095,11 +2084,7 @@ function addRecords(aiTable, trackableEntity, options) {
2095
2084
  const hiddenRecordIds = [];
2096
2085
  let needCopyGroupValuesMap = null;
2097
2086
  if (groups?.length && options.forGroupId) {
2098
- const parentGroups = getParentLinearRowGroups(aiTable, options.forGroupId);
2099
- needCopyGroupValuesMap = parentGroups.reduce((pre, cur) => {
2100
- pre[cur.fieldId] = cur.groupValue;
2101
- return pre;
2102
- }, {});
2087
+ needCopyGroupValuesMap = getParentGroupValuesByGroupId(aiTable, options.forGroupId);
2103
2088
  }
2104
2089
  const records = aiTable.gridData().records;
2105
2090
  newRecordIds.forEach((id, index) => {
@@ -2438,5 +2423,5 @@ const VIEW_ACTIONS = [ActionName.SetView, ActionName.AddView, ActionName.RemoveV
2438
2423
  * Generated bundle index. Do not edit.
2439
2424
  */
2440
2425
 
2441
- export { AITableStateI18nKey, AITableStateI18nText, Actions, CopyCellsItem, CopyFieldPropertyItem, DividerMenuItem, EditFieldPropertyItem, FLUSHING, GroupCalculator, InsertDownwardRecords, InsertUpwardRecords, PasteCellsItem, RemoveRecordsItem, UndoManagerService, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyActions, applyEvents, applyYjsEvents, buildFieldsByView, buildGroupLinearRows, buildRecordsByView, buildRemoveFieldItem, buildSetFieldAction, buildSetRecordPositionsActon, calculateAdaptiveFrozenColumnCount, checkConditions, createMultiplePositions, createPositions, createSharedType, doFilter, findNextRecordForTargetInOriginalRecords, findPrevRecordForTargetInOriginalRecords, freezeToThisColumn, generateCopyName, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getFrozenFieldId, getMaxPosition, getNewRecordsPosition, getParentLinearRowGroups, getPosition, getPositionByAfterOrBeforeRecordId, getPositions, getRecordsBySharedJson, getSharedTypeByData, getSortFields, getSortRecords, getStateI18nTextByKey, isPathEqual, moveFields, moveRecords, removeView, restoreDefaultFrozenColumn, sortRecordsBySortInfo, sortViews, toSharedType, translateYjsEvent, updateFieldAndValues, updateFieldValues, withState };
2426
+ export { AITableStateI18nKey, AITableStateI18nText, Actions, CopyCellsItem, CopyFieldPropertyItem, DividerMenuItem, EditFieldPropertyItem, FLUSHING, GroupCalculator, InsertDownwardRecords, InsertUpwardRecords, PasteCellsItem, RemoveRecordsItem, UndoManagerService, VIEW_ACTIONS, YjsAITable, actionMappers, addFields, addRecords, addView, applyActionOps, applyActions, applyEvents, applyYjsEvents, buildFieldsByView, buildGroupLinearRows, buildRecordsByView, buildRemoveFieldItem, buildSetFieldAction, buildSetRecordPositionsActon, calculateAdaptiveFrozenColumnCount, checkConditions, createMultiplePositions, createPositions, createSharedType, doFilter, findNextRecordForTargetInOriginalRecords, findPrevRecordForTargetInOriginalRecords, freezeToThisColumn, generateCopyName, getDataBySharedType, getDefaultI18nTextByKey, getDefaultRecordDataByFilter, getDefaultRecordValues, getFieldPositionInView, getFieldsSizeMap, getFilteredRecords, getFrozenFieldId, getMaxPosition, getNewRecordsPosition, getParentGroupValuesByGroupId, getParentLinearRowGroups, getPosition, getPositionByAfterOrBeforeRecordId, getPositions, getRecordsBySharedJson, getSharedTypeByData, getSortFields, getSortRecords, getStateI18nTextByKey, isPathEqual, moveFields, moveRecords, removeView, restoreDefaultFrozenColumn, sortRecordsBySortInfo, sortViews, toSharedType, translateYjsEvent, updateFieldAndValues, updateFieldValues, withState };
2442
2427
  //# sourceMappingURL=ai-table-state.mjs.map