@shadow-garden/bapbong-commands 0.1.0 → 0.2.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/dist/index.cjs CHANGED
@@ -26,6 +26,7 @@ __export(index_exports, {
26
26
  activeFontSize: () => activeFontSize,
27
27
  activeHighlight: () => activeHighlight,
28
28
  activeMarkValue: () => activeMarkValue,
29
+ activeParagraphStyle: () => activeParagraphStyle,
29
30
  activeTextColor: () => activeTextColor,
30
31
  cellAt: () => cellAt,
31
32
  clearMarks: () => clearMarks,
@@ -55,6 +56,7 @@ __export(index_exports, {
55
56
  setHighlight: () => setHighlight,
56
57
  setLink: () => setLink,
57
58
  setMarkAttr: () => setMarkAttr,
59
+ setParagraphStyle: () => setParagraphStyle,
58
60
  setTextColor: () => setTextColor,
59
61
  toggleHeading: () => toggleHeading,
60
62
  toggleList: () => toggleList,
@@ -204,7 +206,8 @@ function setAlign(align) {
204
206
  const tr = state.tr;
205
207
  let changed = false;
206
208
  state.doc.nodesBetween(from, to, (node, pos) => {
207
- if (node.type.name !== "paragraph" || node.attrs["align"] === align) return;
209
+ if (node.type.name !== "paragraph" || node.attrs["align"] === align)
210
+ return;
208
211
  tr.setNodeAttribute(pos, "align", align);
209
212
  changed = true;
210
213
  });
@@ -229,7 +232,9 @@ function toggleHeading(level) {
229
232
  const paras = selectedParagraphs(state);
230
233
  if (paras.length === 0) return false;
231
234
  if (dispatch) {
232
- const allThisLevel = paras.every((p) => p.node.attrs["heading"] === level);
235
+ const allThisLevel = paras.every(
236
+ (p) => p.node.attrs["heading"] === level
237
+ );
233
238
  const next = allThisLevel ? null : level;
234
239
  const tr = state.tr;
235
240
  for (const p of paras) tr.setNodeAttribute(p.pos, "heading", next);
@@ -244,6 +249,49 @@ function toggleHeading(level) {
244
249
  isEnabled: (state) => selectedParagraphs(state).length > 0
245
250
  };
246
251
  }
252
+ var STYLE_ATTRS = {
253
+ normal: { heading: null, styleId: null },
254
+ title: { heading: null, styleId: "Title" },
255
+ subtitle: { heading: null, styleId: "Subtitle" },
256
+ h1: { heading: 1, styleId: null },
257
+ h2: { heading: 2, styleId: null },
258
+ h3: { heading: 3, styleId: null }
259
+ };
260
+ function setParagraphStyle(key) {
261
+ const attrs = STYLE_ATTRS[key];
262
+ return {
263
+ name: `paragraph-style-${key}`,
264
+ run(state, dispatch) {
265
+ const paras = selectedParagraphs(state);
266
+ if (paras.length === 0) return false;
267
+ if (dispatch) {
268
+ const tr = state.tr;
269
+ for (const p of paras) {
270
+ tr.setNodeAttribute(p.pos, "heading", attrs.heading);
271
+ tr.setNodeAttribute(p.pos, "styleId", attrs.styleId);
272
+ }
273
+ dispatch(tr.scrollIntoView());
274
+ }
275
+ return true;
276
+ },
277
+ isActive: (state) => activeParagraphStyle(state) === key,
278
+ isEnabled: (state) => selectedParagraphs(state).length > 0
279
+ };
280
+ }
281
+ function activeParagraphStyle(state) {
282
+ const paras = selectedParagraphs(state);
283
+ if (paras.length === 0) return null;
284
+ const keyOf = (node) => {
285
+ const styleId = node.attrs["styleId"];
286
+ if (styleId === "Title") return "title";
287
+ if (styleId === "Subtitle") return "subtitle";
288
+ const h = node.attrs["heading"];
289
+ if (h === 1 || h === 2 || h === 3) return `h${h}`;
290
+ return h == null ? "normal" : null;
291
+ };
292
+ const first = keyOf(paras[0].node);
293
+ return paras.every((p) => keyOf(p.node) === first) ? first : null;
294
+ }
247
295
 
248
296
  // packages/commands/src/lib/list.ts
249
297
  var NUM_ID = { bullet: "bb-bullet", ordered: "bb-ordered" };
@@ -781,6 +829,7 @@ function defaultCommands() {
781
829
  activeFontSize,
782
830
  activeHighlight,
783
831
  activeMarkValue,
832
+ activeParagraphStyle,
784
833
  activeTextColor,
785
834
  cellAt,
786
835
  clearMarks,
@@ -810,6 +859,7 @@ function defaultCommands() {
810
859
  setHighlight,
811
860
  setLink,
812
861
  setMarkAttr,
862
+ setParagraphStyle,
813
863
  setTextColor,
814
864
  toggleHeading,
815
865
  toggleList,
package/dist/index.js CHANGED
@@ -139,7 +139,8 @@ function setAlign(align) {
139
139
  const tr = state.tr;
140
140
  let changed = false;
141
141
  state.doc.nodesBetween(from, to, (node, pos) => {
142
- if (node.type.name !== "paragraph" || node.attrs["align"] === align) return;
142
+ if (node.type.name !== "paragraph" || node.attrs["align"] === align)
143
+ return;
143
144
  tr.setNodeAttribute(pos, "align", align);
144
145
  changed = true;
145
146
  });
@@ -164,7 +165,9 @@ function toggleHeading(level) {
164
165
  const paras = selectedParagraphs(state);
165
166
  if (paras.length === 0) return false;
166
167
  if (dispatch) {
167
- const allThisLevel = paras.every((p) => p.node.attrs["heading"] === level);
168
+ const allThisLevel = paras.every(
169
+ (p) => p.node.attrs["heading"] === level
170
+ );
168
171
  const next = allThisLevel ? null : level;
169
172
  const tr = state.tr;
170
173
  for (const p of paras) tr.setNodeAttribute(p.pos, "heading", next);
@@ -179,6 +182,49 @@ function toggleHeading(level) {
179
182
  isEnabled: (state) => selectedParagraphs(state).length > 0
180
183
  };
181
184
  }
185
+ var STYLE_ATTRS = {
186
+ normal: { heading: null, styleId: null },
187
+ title: { heading: null, styleId: "Title" },
188
+ subtitle: { heading: null, styleId: "Subtitle" },
189
+ h1: { heading: 1, styleId: null },
190
+ h2: { heading: 2, styleId: null },
191
+ h3: { heading: 3, styleId: null }
192
+ };
193
+ function setParagraphStyle(key) {
194
+ const attrs = STYLE_ATTRS[key];
195
+ return {
196
+ name: `paragraph-style-${key}`,
197
+ run(state, dispatch) {
198
+ const paras = selectedParagraphs(state);
199
+ if (paras.length === 0) return false;
200
+ if (dispatch) {
201
+ const tr = state.tr;
202
+ for (const p of paras) {
203
+ tr.setNodeAttribute(p.pos, "heading", attrs.heading);
204
+ tr.setNodeAttribute(p.pos, "styleId", attrs.styleId);
205
+ }
206
+ dispatch(tr.scrollIntoView());
207
+ }
208
+ return true;
209
+ },
210
+ isActive: (state) => activeParagraphStyle(state) === key,
211
+ isEnabled: (state) => selectedParagraphs(state).length > 0
212
+ };
213
+ }
214
+ function activeParagraphStyle(state) {
215
+ const paras = selectedParagraphs(state);
216
+ if (paras.length === 0) return null;
217
+ const keyOf = (node) => {
218
+ const styleId = node.attrs["styleId"];
219
+ if (styleId === "Title") return "title";
220
+ if (styleId === "Subtitle") return "subtitle";
221
+ const h = node.attrs["heading"];
222
+ if (h === 1 || h === 2 || h === 3) return `h${h}`;
223
+ return h == null ? "normal" : null;
224
+ };
225
+ const first = keyOf(paras[0].node);
226
+ return paras.every((p) => keyOf(p.node) === first) ? first : null;
227
+ }
182
228
 
183
229
  // packages/commands/src/lib/list.ts
184
230
  var NUM_ID = { bullet: "bb-bullet", ordered: "bb-ordered" };
@@ -715,6 +761,7 @@ export {
715
761
  activeFontSize,
716
762
  activeHighlight,
717
763
  activeMarkValue,
764
+ activeParagraphStyle,
718
765
  activeTextColor,
719
766
  cellAt,
720
767
  clearMarks,
@@ -744,6 +791,7 @@ export {
744
791
  setHighlight,
745
792
  setLink,
746
793
  setMarkAttr,
794
+ setParagraphStyle,
747
795
  setTextColor,
748
796
  toggleHeading,
749
797
  toggleList,
@@ -22,4 +22,16 @@ export declare function setAlign(align: Align | null): Command;
22
22
  * mirror) / a Word "Heading N" style on export.
23
23
  */
24
24
  export declare function toggleHeading(level: number): Command;
25
+ /** The six named paragraph styles the toolbar dropdown offers. */
26
+ export type ParagraphStyleKey = 'normal' | 'title' | 'subtitle' | 'h1' | 'h2' | 'h3';
27
+ /**
28
+ * Set (not toggle — dropdowns state absolutes) the named paragraph style on
29
+ * every selected paragraph: Normal / Title / Subtitle / Heading 1–3. The only
30
+ * writer of `styleId`, so heading and styleId can never both be set.
31
+ */
32
+ export declare function setParagraphStyle(key: ParagraphStyleKey): Command;
33
+ /** The dropdown's current value: the shared style of every selected paragraph,
34
+ * or null when the selection is empty / mixed / on an unlisted style (H4–H6).
35
+ * O(selection) — it only walks the selected range (called per transaction). */
36
+ export declare function activeParagraphStyle(state: EditorState): ParagraphStyleKey | null;
25
37
  //# sourceMappingURL=paragraph.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"paragraph.d.ts","sourceRoot":"","sources":["../../src/lib/paragraph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kCAAkC,CAAC;AAEhE,qFAAqF;AACrF,eAAO,MAAM,UAAU,iDAAkD,CAAC;AAC1E,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC;AAEhD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,KAAK,GAAG,IAAI,CAS5D;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,GAAG,OAAO,CAiBrD;AAYD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAqBpD"}
1
+ {"version":3,"file":"paragraph.d.ts","sourceRoot":"","sources":["../../src/lib/paragraph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kCAAkC,CAAC;AAEhE,qFAAqF;AACrF,eAAO,MAAM,UAAU,iDAAkD,CAAC;AAC1E,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC;AAEhD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,KAAK,GAAG,IAAI,CAS5D;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,GAAG,OAAO,CAkBrD;AAcD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CA0BpD;AAED,kEAAkE;AAClE,MAAM,MAAM,iBAAiB,GACzB,QAAQ,GACR,OAAO,GACP,UAAU,GACV,IAAI,GACJ,IAAI,GACJ,IAAI,CAAC;AAgBT;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,iBAAiB,GAAG,OAAO,CAoBjE;AAED;;gFAEgF;AAChF,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,WAAW,GACjB,iBAAiB,GAAG,IAAI,CAe1B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shadow-garden/bapbong-commands",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "bapbong — headless editor commands + queries + registry (isomorphic; backend and UI share the same ops)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -60,6 +60,6 @@
60
60
  "prosemirror-history": "^1.4.1",
61
61
  "prosemirror-model": "^1.25.7",
62
62
  "prosemirror-state": "^1.4.4",
63
- "@shadow-garden/bapbong-contracts": "^0.1.0"
63
+ "@shadow-garden/bapbong-contracts": "^0.1.1"
64
64
  }
65
65
  }