@medyll/idae-be 0.80.0 → 0.81.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.
@@ -17,6 +17,11 @@ declare enum domMethods {
17
17
  }
18
18
  type Content = string | HTMLElement | Be;
19
19
  export interface DomHandlerHandle {
20
+ insert?: {
21
+ content: string | HTMLElement | Be;
22
+ mode: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend';
23
+ callback?: (element: HandlerCallbackProps) => void;
24
+ };
20
25
  update?: {
21
26
  content: Content;
22
27
  callback?: (element: HandlerCallbackProps) => void;
@@ -80,8 +85,8 @@ export declare class DomHandler implements DomHandlerInterface, CommonHandler<Do
80
85
  update(content: string, callback?: HandlerCallBackFn): Be;
81
86
  append(content: Content, callback?: HandlerCallBackFn): Be;
82
87
  prepend(content: Content, callback?: HandlerCallBackFn): Be;
83
- insert(mode: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', element: HTMLElement | Be, callback?: HandlerCallBackFn): Be;
84
- afterBegin(content: HTMLElement, callback?: HandlerCallBackFn): Be;
88
+ insert(mode: 'afterbegin' | 'afterend' | 'beforebegin' | 'beforeend', element: HTMLElement | Be | string, callback?: HandlerCallBackFn): Be;
89
+ afterBegin(content: Content, callback?: HandlerCallBackFn): Be;
85
90
  afterEnd(content: Content, callback?: HandlerCallBackFn): Be;
86
91
  beforeBegin(content: Content, callback?: HandlerCallBackFn): Be;
87
92
  beforeEnd(content: Content, callback?: HandlerCallBackFn): Be;
@@ -91,6 +96,8 @@ export declare class DomHandler implements DomHandlerInterface, CommonHandler<Do
91
96
  normalize(callback?: HandlerCallBackFn): Be;
92
97
  wrap(tag?: string, callback?: HandlerCallBackFn): Be;
93
98
  private adjacentElement;
99
+ private normalizeContent;
100
+ private insertContent;
94
101
  valueOf(): string | null;
95
102
  }
96
103
  export {};
@@ -79,15 +79,13 @@ export class DomHandler {
79
79
  append(content, callback) {
80
80
  const ret = [];
81
81
  this.beElement.eachNode((el) => {
82
- if (content instanceof Be) {
83
- content.eachNode((child) => {
84
- ret.push(child);
85
- el.appendChild(child);
86
- });
82
+ const normalizedContent = this.normalizeContent(content);
83
+ if (normalizedContent instanceof DocumentFragment) {
84
+ el.appendChild(normalizedContent);
87
85
  }
88
86
  else {
89
- ret.push(content);
90
- el.appendChild(content);
87
+ ret.push(normalizedContent);
88
+ el.appendChild(normalizedContent);
91
89
  }
92
90
  });
93
91
  callback?.({
@@ -100,15 +98,13 @@ export class DomHandler {
100
98
  prepend(content, callback) {
101
99
  const ret = [];
102
100
  this.beElement.eachNode((el) => {
103
- if (content instanceof Be) {
104
- content.eachNode((child) => {
105
- ret.push(child);
106
- el.insertBefore(child, el.firstChild);
107
- });
101
+ const normalizedContent = this.normalizeContent(content);
102
+ if (normalizedContent instanceof DocumentFragment) {
103
+ el.insertBefore(normalizedContent, el.firstChild);
108
104
  }
109
105
  else {
110
- ret.push(content);
111
- el.insertBefore(content, el.firstChild);
106
+ ret.push(normalizedContent);
107
+ el.insertBefore(normalizedContent, el.firstChild);
112
108
  }
113
109
  });
114
110
  callback?.({
@@ -128,6 +124,8 @@ export class DomHandler {
128
124
  return this.beforeBegin(element, callback);
129
125
  case 'beforeend':
130
126
  return this.beforeEnd(element, callback);
127
+ default:
128
+ throw new Error(`Invalid mode: ${mode}`);
131
129
  }
132
130
  }
133
131
  afterBegin(content, callback) {
@@ -142,29 +140,51 @@ export class DomHandler {
142
140
  return this.beElement;
143
141
  }
144
142
  afterEnd(content, callback) {
145
- this.append(content, callback);
143
+ this.beElement.eachNode((el) => {
144
+ // Insérer après l'élément cible
145
+ el.parentNode?.insertBefore(this.normalizeContent(content), el.nextSibling);
146
+ callback?.({
147
+ fragment: content,
148
+ be: be(el),
149
+ root: this.beElement
150
+ });
151
+ });
146
152
  return this.beElement;
147
153
  }
148
154
  beforeBegin(content, callback) {
149
- this.prepend(content, callback);
155
+ this.beElement.eachNode((el) => {
156
+ // Insérer avant l'élément cible
157
+ el.parentNode?.insertBefore(this.normalizeContent(content), el);
158
+ callback?.({
159
+ fragment: content,
160
+ be: be(el),
161
+ root: this.beElement
162
+ });
163
+ });
150
164
  return this.beElement;
151
165
  }
152
166
  beforeEnd(content, callback) {
153
- this.append(content, callback);
167
+ this.beElement.eachNode((el) => {
168
+ // Insérer à la fin de l'élément cible
169
+ el.appendChild(this.normalizeContent(content));
170
+ callback?.({
171
+ fragment: content,
172
+ be: be(el),
173
+ root: this.beElement
174
+ });
175
+ });
154
176
  return this.beElement;
155
177
  }
156
178
  replace(content, callback) {
157
179
  const ret = [];
158
180
  this.beElement.eachNode((el) => {
159
- if (content instanceof Be) {
160
- content.eachNode((child) => {
161
- ret.push(child);
162
- el.replaceWith(child);
163
- });
181
+ const normalizedContent = this.normalizeContent(content);
182
+ if (normalizedContent instanceof DocumentFragment) {
183
+ el.replaceWith(...normalizedContent.childNodes);
164
184
  }
165
185
  else {
166
- ret.push(content);
167
- el.replaceWith(content);
186
+ ret.push(normalizedContent);
187
+ el.replaceWith(normalizedContent);
168
188
  }
169
189
  });
170
190
  callback?.({
@@ -223,8 +243,74 @@ export class DomHandler {
223
243
  return this.beElement;
224
244
  }
225
245
  adjacentElement(element, content, mode) {
226
- element.insertAdjacentElement(mode, content);
227
- return this.beElement;
246
+ const normalizedContent = this.normalizeContent(content);
247
+ if (typeof content === 'string') {
248
+ // Si le contenu est une chaîne HTML, utilisez insertAdjacentHTML
249
+ element.insertAdjacentHTML(mode, content);
250
+ }
251
+ else if (normalizedContent instanceof HTMLElement) {
252
+ // Si le contenu est un élément DOM, utilisez insertAdjacentElement
253
+ if (mode === 'afterend') {
254
+ element.parentNode?.insertBefore(normalizedContent, element.nextSibling);
255
+ }
256
+ else if (mode === 'beforebegin') {
257
+ element.parentNode?.insertBefore(normalizedContent, element);
258
+ }
259
+ else {
260
+ element.insertAdjacentElement(mode, normalizedContent);
261
+ }
262
+ }
263
+ else if (normalizedContent instanceof DocumentFragment) {
264
+ // Si le contenu est un fragment, insérez chaque nœud
265
+ Array.from(normalizedContent.childNodes).forEach((node) => {
266
+ if (mode === 'afterbegin' || mode === 'beforeend') {
267
+ element.appendChild(node);
268
+ }
269
+ else if (mode === 'afterend') {
270
+ element.parentNode?.insertBefore(node, element.nextSibling);
271
+ }
272
+ else if (mode === 'beforebegin') {
273
+ element.parentNode?.insertBefore(node, element);
274
+ }
275
+ });
276
+ }
277
+ }
278
+ normalizeContent(content) {
279
+ if (typeof content === 'string') {
280
+ // Si le contenu est une chaîne HTML, créez un fragment DOM
281
+ const template = document.createElement('template');
282
+ template.innerHTML = content.trim();
283
+ return template.content;
284
+ }
285
+ else if (content instanceof Be) {
286
+ // Si le contenu est une instance de Be, utilisez son premier nœud
287
+ if (Array.isArray(content.node)) {
288
+ const fragment = document.createDocumentFragment();
289
+ content.node.forEach((node) => {
290
+ if (node instanceof HTMLElement) {
291
+ fragment.appendChild(node);
292
+ }
293
+ });
294
+ return fragment;
295
+ }
296
+ else if (content.node instanceof HTMLElement) {
297
+ return content.node;
298
+ }
299
+ throw new Error('Invalid Be instance: no valid node found.');
300
+ }
301
+ else {
302
+ // Sinon, le contenu est déjà un HTMLElement
303
+ return content;
304
+ }
305
+ }
306
+ insertContent(el, content, mode) {
307
+ const normalizedContent = this.normalizeContent(content);
308
+ if (mode === 'afterEnd') {
309
+ el.parentNode?.insertBefore(normalizedContent, el.nextSibling);
310
+ }
311
+ else if (mode === 'beforeEnd') {
312
+ el.appendChild(normalizedContent);
313
+ }
228
314
  }
229
315
  valueOf() {
230
316
  if (this.beElement.isWhat !== 'element')
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@medyll/idae-be",
3
3
  "scope": "@medyll",
4
- "version": "0.80.0",
4
+ "version": "0.81.0",
5
5
  "description": "A powerful DOM manipulation library with a callback-based approach for precise element targeting. Provides consistent chaining, comprehensive DOM traversal, event handling, style management, and more. Written in TypeScript for modern browsers.",
6
6
  "scripts": {
7
7
  "dev": "vite dev",