pages_core 3.15.1 → 3.15.3

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.
@@ -6,10 +6,17 @@ export default function useAttachments(
6
6
  records: Attachments.Record[]
7
7
  ): Attachments.State {
8
8
  const [deleted, setDeleted] = useState<Attachments.Record[]>([]);
9
+ const collection = useDragCollection(records);
10
+
11
+ const update = (records: Attachments.Record[]) => {
12
+ collection.dispatch({ type: "reinitialize", payload: records });
13
+ setDeleted([]);
14
+ };
9
15
 
10
16
  return {
11
- collection: useDragCollection(records),
17
+ collection: collection,
12
18
  deleted: deleted,
13
- setDeleted: setDeleted
19
+ setDeleted: setDeleted,
20
+ update: update
14
21
  };
15
22
  }
@@ -4,23 +4,39 @@ import * as Images from "../../types/Images";
4
4
 
5
5
  import { useDragCollection } from "../drag";
6
6
 
7
- export default function useImageGrid(
8
- records: Images.Record[],
9
- enablePrimary = false
10
- ): Images.GridState {
7
+ function filterRecords(records: Images.Record[], enablePrimary = false) {
11
8
  const primaryRecords = enablePrimary
12
9
  ? records.filter((r) => r.primary).slice(0, 1)
13
10
  : [];
14
11
  const imageRecords = records.filter((r) => primaryRecords.indexOf(r) === -1);
12
+ return [primaryRecords, imageRecords];
13
+ }
14
+
15
+ export default function useImageGrid(
16
+ records: Images.Record[],
17
+ enablePrimary = false
18
+ ): Images.GridState {
19
+ const [primaryRecords, imageRecords] = filterRecords(records, enablePrimary);
15
20
 
16
21
  const primary = useDragCollection(primaryRecords);
17
22
  const images = useDragCollection(imageRecords);
18
23
  const [deleted, setDeleted] = useState<Images.Record[]>([]);
19
24
 
25
+ const update = (records: Images.Record[]) => {
26
+ const [primaryRecords, imageRecords] = filterRecords(
27
+ records,
28
+ enablePrimary
29
+ );
30
+ primary.dispatch({ type: "reinitialize", payload: primaryRecords });
31
+ images.dispatch({ type: "reinitialize", payload: imageRecords });
32
+ setDeleted([]);
33
+ };
34
+
20
35
  return {
21
36
  primary: primary,
22
37
  images: images,
23
38
  deleted: deleted,
24
- setDeleted: setDeleted
39
+ setDeleted: setDeleted,
40
+ update: update
25
41
  };
26
42
  }
@@ -82,11 +82,6 @@ export default function PageForm(props: Props) {
82
82
  });
83
83
  };
84
84
 
85
- const clearDeletedObjects = () => {
86
- filesState.setDeleted([]);
87
- imagesState.setDeleted([]);
88
- };
89
-
90
85
  const handleSubmit = (evt: React.MouseEvent) => {
91
86
  evt.preventDefault();
92
87
  let method = postJson;
@@ -110,7 +105,8 @@ export default function PageForm(props: Props) {
110
105
  if (response.errors && response.errors.length > 0) {
111
106
  errorToast("A validation error prevented the page from being saved.");
112
107
  } else {
113
- clearDeletedObjects();
108
+ filesState.update(response.page_files);
109
+ imagesState.update(response.page_images);
114
110
  noticeToast("Your changes were saved");
115
111
  }
116
112
  })
@@ -12,10 +12,15 @@ function getPosition<T>(draggable: Drag.Draggable<T>) {
12
12
  }
13
13
 
14
14
  function hideDraggable<T>(
15
- draggable: Drag.Draggable<T> | null,
16
- callback: () => Drag.Draggable<T>[]
15
+ draggable: Drag.Item<T> | null,
16
+ callback: () => Drag.Item<T>[]
17
17
  ) {
18
- if (draggable && "ref" in draggable && draggable.ref.current) {
18
+ if (
19
+ draggable &&
20
+ draggable !== "Files" &&
21
+ "ref" in draggable &&
22
+ draggable.ref.current
23
+ ) {
19
24
  const prevDisplay = draggable.ref.current.style.display;
20
25
  draggable.ref.current.style.display = "none";
21
26
  const result = callback();
@@ -61,6 +66,8 @@ function dragCollectionReducer<T = Drag.DraggableRecord>(
61
66
  return { ...d, rect: getPosition(d) };
62
67
  });
63
68
  });
69
+ case "reinitialize":
70
+ return action.payload.map((r) => createDraggable(r));
64
71
  case "remove":
65
72
  return state.filter(
66
73
  (d: Drag.Draggable<T>) => d.handle !== action.payload.handle
@@ -42,7 +42,7 @@ function mousePosition(evt: AnyTouchEvent): Drag.Position {
42
42
  if ("touches" in evt && evt.type == "touchmove") {
43
43
  x = evt.touches[0].clientX;
44
44
  y = evt.touches[0].clientY;
45
- } else if (evt instanceof MouseEvent) {
45
+ } else if ("clientX" in evt && "clientY" in evt) {
46
46
  x = evt.clientX;
47
47
  y = evt.clientY;
48
48
  }
@@ -26,4 +26,5 @@ export interface State {
26
26
  collection: Drag.Collection<Record>;
27
27
  deleted: Record[];
28
28
  setDeleted: (records: Record[]) => void;
29
+ update: (records: Record[]) => void;
29
30
  }
@@ -15,6 +15,7 @@ export type CollectionAction<T = DraggableRecord> =
15
15
  payload: Item<T>[];
16
16
  }
17
17
  | { type: "update"; payload: Item<T> }
18
+ | { type: "reinitialize"; payload: Array<T> }
18
19
  | { type: "remove"; payload: Draggable<T> }
19
20
  | { type: "updatePositions"; payload?: Draggable<T> };
20
21
 
@@ -44,4 +44,5 @@ export interface GridState {
44
44
  images: Drag.Collection<Record>;
45
45
  deleted: Record[];
46
46
  setDeleted: (records: Record[]) => void;
47
+ update: (records: Record[]) => void;
47
48
  }
@@ -33,6 +33,11 @@ module PagesCore
33
33
  super.in_locale(locale)
34
34
  end
35
35
 
36
+ def page_files_attributes=(attrs)
37
+ ids = page_files.map(&:id)
38
+ super(attrs.reject { |a| a["_destroy"] && ids.exclude?(a["id"]) })
39
+ end
40
+
36
41
  def files
37
42
  page_files
38
43
  end
@@ -19,11 +19,11 @@ module PagesCore
19
19
 
20
20
  after_save :update_primary_image
21
21
 
22
- accepts_nested_attributes_for :page_images,
23
- reject_if: proc { |a|
24
- a["image_id"].blank?
25
- },
26
- allow_destroy: true
22
+ accepts_nested_attributes_for(
23
+ :page_images,
24
+ reject_if: proc { |a| a["image_id"].blank? },
25
+ allow_destroy: true
26
+ )
27
27
  end
28
28
 
29
29
  def image?
@@ -42,6 +42,11 @@ module PagesCore
42
42
  super.in_locale(locale)
43
43
  end
44
44
 
45
+ def page_images_attributes=(attrs)
46
+ ids = page_images.map(&:id)
47
+ super(attrs.reject { |a| a["_destroy"] && ids.exclude?(a["id"]) })
48
+ end
49
+
45
50
  private
46
51
 
47
52
  def update_primary_image
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pages_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.15.1
4
+ version: 3.15.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inge Jørgensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-07 00:00:00.000000000 Z
11
+ date: 2024-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails