@replit/codemirror-interact
Advanced tools
Comparing version 6.0.2 to 6.0.3
import { Decoration, EditorView, ViewPlugin } from '@codemirror/view'; | ||
import { StateEffect, Facet } from '@codemirror/state'; | ||
import { StateEffect, Facet, Compartment, Prec } from '@codemirror/state'; | ||
// TODO: don't use document.style.cursor | ||
// TODO: custom style | ||
const mark = /*@__PURE__*/Decoration.mark({ class: 'cm-interact' }); | ||
@@ -37,2 +37,8 @@ const setInteract = /*@__PURE__*/StateEffect.define(); | ||
}); | ||
const setStyle = (style = '') => EditorView.contentAttributes.of({ style }); | ||
const normalCursor = /*@__PURE__*/setStyle(); | ||
const cursorCompartment = /*@__PURE__*/new Compartment(); | ||
const cursorRule = /*@__PURE__*/Prec.highest(/*@__PURE__*/cursorCompartment.of(normalCursor)); | ||
const clearCursor = () => cursorCompartment.reconfigure(normalCursor); | ||
const setCursor = (cursor) => cursor ? [cursorCompartment.reconfigure(setStyle(`cursor: ${cursor}`))] : []; | ||
const interactViewPlugin = /*@__PURE__*/ViewPlugin.define((view) => ({ | ||
@@ -90,13 +96,9 @@ dragging: null, | ||
highlight(target) { | ||
if (target.rule.cursor) { | ||
document.body.style.cursor = target.rule.cursor; | ||
} | ||
view.dispatch({ | ||
effects: setInteract.of(target), | ||
effects: [setInteract.of(target), ...setCursor(target.rule.cursor)], | ||
}); | ||
}, | ||
unhighlight() { | ||
document.body.style.cursor = 'auto'; | ||
view.dispatch({ | ||
effects: setInteract.of(null), | ||
effects: [setInteract.of(null), clearCursor()], | ||
}); | ||
@@ -198,2 +200,3 @@ }, | ||
interactModKey.of((_a = cfg.key) !== null && _a !== void 0 ? _a : "alt"), | ||
cursorRule, | ||
((_b = cfg.rules) !== null && _b !== void 0 ? _b : []).map((r) => interactRule.of(r)), | ||
@@ -200,0 +203,0 @@ ]; |
{ | ||
"name": "@replit/codemirror-interact", | ||
"description": "Interact with custom values", | ||
"version": "6.0.2", | ||
"version": "6.0.3", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -1,2 +0,1 @@ | ||
// TODO: don't use document.style.cursor | ||
// TODO: custom style | ||
@@ -14,2 +13,4 @@ // TODO: custom state for each rule? | ||
Facet, | ||
Prec, | ||
Compartment, | ||
} from '@codemirror/state' | ||
@@ -67,2 +68,14 @@ | ||
const setStyle = (style = '') => | ||
EditorView.contentAttributes.of({ style }); | ||
const normalCursor = setStyle(); | ||
const cursorCompartment = new Compartment(); | ||
const cursorRule = Prec.highest(cursorCompartment.of(normalCursor)); | ||
const clearCursor = () => cursorCompartment.reconfigure(normalCursor); | ||
const setCursor = (cursor?: string) => | ||
cursor ? [cursorCompartment.reconfigure(setStyle(`cursor: ${cursor}`))] : []; | ||
interface ViewState extends PluginValue { | ||
@@ -137,7 +150,4 @@ dragging: Target | null, | ||
highlight(target) { | ||
if (target.rule.cursor) { | ||
document.body.style.cursor = target.rule.cursor; | ||
} | ||
view.dispatch({ | ||
effects: setInteract.of(target), | ||
effects: [setInteract.of(target), ...setCursor(target.rule.cursor)], | ||
}); | ||
@@ -147,5 +157,4 @@ }, | ||
unhighlight() { | ||
document.body.style.cursor = 'auto' | ||
view.dispatch({ | ||
effects: setInteract.of(null), | ||
effects: [setInteract.of(null), clearCursor()], | ||
}); | ||
@@ -274,2 +283,3 @@ }, | ||
interactModKey.of(cfg.key ?? "alt"), | ||
cursorRule, | ||
(cfg.rules ?? []).map((r) => interactRule.of(r)), | ||
@@ -276,0 +286,0 @@ ]; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23764
682