Files
RuneStone/src/lib/editor/imageDrop.ts
T

19 lines
765 B
TypeScript

import { StateEffect, StateField } from "@codemirror/state";
export const trackImageDrop = StateEffect.define<{ id: symbol; from: number; to: number }>();
export const clearImageDrop = StateEffect.define<symbol>();
export const imageDrops = StateField.define<Map<symbol, { from: number; to: number }>>({
create: () => new Map(),
update(value, transaction) {
const next = new Map([...value].map(([id, range]) => [id, {
from: transaction.changes.mapPos(range.from, 1),
to: transaction.changes.mapPos(range.to, 1),
}]));
for (const effect of transaction.effects) {
if (effect.is(trackImageDrop)) next.set(effect.value.id, effect.value);
if (effect.is(clearImageDrop)) next.delete(effect.value);
}
return next;
},
});