|
| 1 | +import { Plugin, PluginKey } from 'prosemirror-state'; |
| 2 | +import { Decoration, DecorationSet } from 'prosemirror-view'; |
| 3 | +import { PaginationPluginKey } from '../../pagination/pagination-helpers.js'; |
| 4 | + |
| 5 | +const ImagePositionPluginKey = new PluginKey('ImagePosition'); |
| 6 | +export const ImagePositionPlugin = ({ editor }) => { |
| 7 | + const { view } = editor; |
| 8 | + let shouldUpdate = false; |
| 9 | + return new Plugin({ |
| 10 | + name: 'ImagePositionPlugin', |
| 11 | + key: ImagePositionPluginKey, |
| 12 | + |
| 13 | + state: { |
| 14 | + init(_, state) { |
| 15 | + return DecorationSet.empty; |
| 16 | + }, |
| 17 | + |
| 18 | + apply(tr, oldDecorationSet, oldState, newState) { |
| 19 | + const decorations = getImagePositionDecorations(newState, view); |
| 20 | + return DecorationSet.create(newState.doc, decorations); |
| 21 | + }, |
| 22 | + }, |
| 23 | + |
| 24 | + view: (view) => { |
| 25 | + return { |
| 26 | + update: (view, lastState) => { |
| 27 | + const pagination = PaginationPluginKey.getState(lastState); |
| 28 | + if (shouldUpdate) { |
| 29 | + shouldUpdate = false; |
| 30 | + const decorations = getImagePositionDecorations(lastState, view); |
| 31 | + const updateTransaction = view.state.tr.setMeta( |
| 32 | + ImagePositionPluginKey, |
| 33 | + { decorations } |
| 34 | + ); |
| 35 | + view.dispatch(updateTransaction); |
| 36 | + } |
| 37 | + if (pagination.isReadyToInit) { |
| 38 | + shouldUpdate = true; |
| 39 | + } |
| 40 | + }, |
| 41 | + }; |
| 42 | + }, |
| 43 | + |
| 44 | + props: { |
| 45 | + decorations(state) { |
| 46 | + return this.getState(state); |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | +}; |
| 51 | + |
| 52 | +const getImagePositionDecorations = (state, view) => { |
| 53 | + let decorations = []; |
| 54 | + state.doc.descendants((node, pos) => { |
| 55 | + if (node.attrs.anchorData) { |
| 56 | + let style = ''; |
| 57 | + let className = ''; |
| 58 | + const { vRelativeFrom, alignH } = node.attrs.anchorData; |
| 59 | + const { size, padding } = node.attrs; |
| 60 | + |
| 61 | + const pageBreak = findPreviousDomNodeWithClass(view, pos, 'pagination-break-wrapper'); |
| 62 | + if (pageBreak) { |
| 63 | + switch (alignH) { |
| 64 | + case 'left': |
| 65 | + style += 'float: left; left: 0; margin-left: 0; '; |
| 66 | + break; |
| 67 | + case 'right': |
| 68 | + style += 'float: right; right: 0; margin-right: 0; '; |
| 69 | + break; |
| 70 | + case 'center': |
| 71 | + style += 'display: block; margin-left: auto; margin-right: auto; '; |
| 72 | + break; |
| 73 | + } |
| 74 | + style += vRelativeFrom === 'margin' ? `position: absolute; top: ${pageBreak?.offsetTop + pageBreak?.offsetHeight}px; ` : ''; |
| 75 | + |
| 76 | + if (vRelativeFrom === 'margin') { |
| 77 | + const nextPos = view.posAtDOM(pageBreak, 1); |
| 78 | + const imageBlock = document.createElement('div'); |
| 79 | + imageBlock.className = 'anchor-image-placeholder'; |
| 80 | + imageBlock.style.float = alignH; |
| 81 | + imageBlock.style.width = size.width + parseInt(padding[alignH]) + 'px'; |
| 82 | + imageBlock.style.height = size.height + parseInt(padding.top) + parseInt(padding.bottom) + 'px'; |
| 83 | + decorations.push(Decoration.widget(nextPos, imageBlock, { key: 'stable-key' })); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + decorations.push( |
| 88 | + Decoration.node(pos, pos + node.nodeSize, { style, class: className }), |
| 89 | + ); |
| 90 | + } |
| 91 | + }); |
| 92 | + return decorations; |
| 93 | +}; |
| 94 | + |
| 95 | +const findPreviousDomNodeWithClass = (view, pos, className) => { |
| 96 | + let { node } = view.domAtPos(pos); |
| 97 | + |
| 98 | + // If you get a text node, go to its parent |
| 99 | + if (node.nodeType === 3) { |
| 100 | + node = node.parentNode; |
| 101 | + } |
| 102 | + |
| 103 | + // Walk backward over siblings and their ancestors |
| 104 | + while (node) { |
| 105 | + if (node.classList && node.classList.contains(className)) { |
| 106 | + return node; |
| 107 | + } |
| 108 | + if (node.previousSibling) { |
| 109 | + node = node.previousSibling; |
| 110 | + // Dive to the last child if it's an element with children |
| 111 | + while (node && node.lastChild) { |
| 112 | + node = node.lastChild; |
| 113 | + } |
| 114 | + } else { |
| 115 | + node = node.parentNode; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return null; // Not found |
| 120 | +} |
0 commit comments