-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @floating-ui for popovers (anchor positioning does not work in Fi…
…refox) and abstract popover behaviour, adding ErrorPopover as inheritor With some tidying to do for popover positioning, display and update
- Loading branch information
1 parent
8a0e4a6
commit 667a1b0
Showing
14 changed files
with
249 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "dependencies": { "@floating-ui/dom": "^1.6.7" } } |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Mapping } from "prosemirror-transform"; | ||
import { EditorView } from "prosemirror-view"; | ||
import { CqlError } from "../services/CqlService"; | ||
import { Popover } from "./Popover"; | ||
|
||
export class ErrorPopover extends Popover { | ||
private debugContainer: HTMLElement | undefined; | ||
|
||
public constructor( | ||
public view: EditorView, | ||
public popoverEl: HTMLElement, | ||
debugEl?: HTMLElement | ||
) { | ||
super(view, popoverEl); | ||
if (debugEl) { | ||
this.debugContainer = document.createElement("div"); | ||
debugEl.appendChild(this.debugContainer); | ||
} | ||
} | ||
|
||
public updateErrorMessage = async ( | ||
error: CqlError | undefined, | ||
mapping: Mapping | ||
) => { | ||
if (!error) { | ||
this.popoverEl.innerHTML = ""; | ||
this.popoverEl.hidePopover(); | ||
return; | ||
} | ||
|
||
this.updateDebugContainer(error); | ||
|
||
this.popoverEl.innerHTML = error.message; | ||
|
||
await this.renderElementAtPos( | ||
error.position ? mapping.map(error.position) : undefined | ||
); | ||
|
||
this.popoverEl.showPopover(); | ||
}; | ||
|
||
private updateDebugContainer = (error: CqlError) => { | ||
if (this.debugContainer) { | ||
this.debugContainer.innerHTML = `<div> | ||
<h2>Error</h3> | ||
<div>Position: ${error.position ?? "No position given"}</div> | ||
<div>Message: ${error.message}</div> | ||
</div>`; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { arrow, computePosition, flip, offset, shift } from "@floating-ui/dom"; | ||
import { EditorView } from "prosemirror-view"; | ||
|
||
export abstract class Popover { | ||
public constructor(public view: EditorView, public popoverEl: HTMLElement) {} | ||
|
||
protected async renderElementAtPos(position: number | undefined) { | ||
const element = this.getVirtualElementFromView(position); | ||
|
||
if (!element) { | ||
return; | ||
} | ||
|
||
const { x, y } = await computePosition(element, this.popoverEl, { | ||
placement: "bottom-start", | ||
middleware: [flip(), shift(), offset({ mainAxis: 15, crossAxis: -30 }), arrow()], | ||
}); | ||
|
||
this.popoverEl.setAttribute("style", `left: ${x}px; top: ${y}px`); | ||
} | ||
|
||
private getVirtualElementFromView = ( | ||
position: number | undefined | ||
): | ||
| undefined | ||
| { | ||
getBoundingClientRect: () => { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
top: number; | ||
left: number; | ||
right: number; | ||
bottom: number; | ||
}; | ||
} => { | ||
if (position) { | ||
try { | ||
const { top, right, bottom, left } = this.view.coordsAtPos(position); | ||
return { | ||
getBoundingClientRect: () => { | ||
const a = { | ||
width: right - left, | ||
height: bottom - top, | ||
x: left, | ||
y: top, | ||
top, | ||
left, | ||
right, | ||
bottom, | ||
}; | ||
console.log(a); | ||
return a; | ||
}, | ||
}; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
return this.view.dom; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.