setListStyle.ts 770 B

1234567891011121314151617181920212223242526272829
  1. import type { EditorView } from 'prosemirror-view';
  2. import { isList } from '../utils';
  3. type Style = Record<string, string>;
  4. export const setListStyle = (view: EditorView, style: Style | Style[]) => {
  5. const { state } = view;
  6. const { schema, selection } = state;
  7. const tr = state.tr.setSelection(selection);
  8. const { doc } = tr;
  9. if (!doc) return tr;
  10. const { from, to } = selection;
  11. doc.nodesBetween(from, to, (node, pos) => {
  12. if (isList(node, schema)) {
  13. if (from - 3 <= pos && to + 3 >= pos + node.nodeSize) {
  14. const styles = Array.isArray(style) ? style : [style];
  15. for (const style of styles) {
  16. tr.setNodeAttribute(pos, style.key, style.value);
  17. }
  18. }
  19. }
  20. return false;
  21. });
  22. view.dispatch(tr);
  23. };