-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gutter enrichment manager #42
Merged
+128
−7
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,122 @@ | ||
import { rangeToTarget, IRangeTarget } from "../ranges"; | ||
|
||
export interface IGutterEnrichmentProvider { | ||
/** If the provider wants to add an enrichment for this target, return a button element. | ||
* @param target | ||
*/ | ||
getButton(target: IRangeTarget): HTMLButtonElement | null; | ||
|
||
/** | ||
* The user clicked the provider's button, add an enrichment for the provided target. | ||
* @param target | ||
*/ | ||
addEnrichment(target: IRangeTarget): void; | ||
} | ||
|
||
/** | ||
* This manager provides support for creating gutter item enrichments from selected text in the document body. | ||
* Providers register themselves with the manager and will be called when a new range is selected. | ||
* | ||
* The manager must be created on an element that has an `la-gutter` and an `la-akoma-ntoso` element as descendants. | ||
*/ | ||
export class GutterEnrichmentManager { | ||
protected root: Element; | ||
protected gutter: Element | null; | ||
protected akn: Element | null; | ||
protected providers: IGutterEnrichmentProvider[]; | ||
protected floaterTimeout: number | null; | ||
protected target: IRangeTarget | null; | ||
protected floatingContainer: HTMLElement; | ||
|
||
constructor (root: Element) { | ||
this.root = root; | ||
this.gutter = root.querySelector('la-gutter'); | ||
this.akn = root.querySelector('la-akoma-ntoso'); | ||
this.providers = []; | ||
this.floatingContainer = this.createFloatingContainer(); | ||
this.floaterTimeout = null; | ||
this.target = null; | ||
document.addEventListener('selectionchange', this.selectionChanged.bind(this)); | ||
} | ||
|
||
addProvider (provider: IGutterEnrichmentProvider) { | ||
this.providers.push(provider); | ||
} | ||
|
||
createFloatingContainer () { | ||
const item = document.createElement('la-gutter-item'); | ||
const btnGroup = document.createElement('div'); | ||
btnGroup.className = 'gutter-enrichment-new-buttons btn-group-vertical btn-group-sm bg-white'; | ||
item.appendChild(btnGroup); | ||
return item; | ||
} | ||
|
||
/** | ||
* When the selection in the document changes, transform it into a target description and, if successful, | ||
* show the floating button container in the gutter. | ||
*/ | ||
selectionChanged () { | ||
const sel = document.getSelection(); | ||
|
||
if (!(this.akn && this.gutter)) { | ||
return; | ||
} | ||
|
||
if (sel && sel.rangeCount > 0 && !sel.getRangeAt(0).collapsed) { | ||
if (this.floaterTimeout) window.clearTimeout(this.floaterTimeout); | ||
|
||
const range = sel.getRangeAt(0); | ||
|
||
// is the common ancestor inside the akn container? | ||
if (range.commonAncestorContainer.compareDocumentPosition(this.akn) & Node.DOCUMENT_POSITION_CONTAINS) { | ||
// find first element | ||
let root: Node | null = range.startContainer; | ||
while (root && root.nodeType !== Node.ELEMENT_NODE) root = root.parentElement; | ||
|
||
// stash the range as converted to a target; this may be null! | ||
this.target = rangeToTarget(range, this.akn); | ||
if (this.target) { | ||
this.addProviderButtons(this.target); | ||
|
||
// @ts-ignore | ||
this.floatingContainer.anchor = root; | ||
|
||
// add it to the gutter if it is not already there | ||
if (!this.gutter.contains(this.floatingContainer)) { | ||
this.gutter.appendChild(this.floatingContainer); | ||
} | ||
} else { | ||
this.removeFloater(); | ||
} | ||
} | ||
} else { | ||
// this needs to stick around for a little bit, for the case | ||
// where the selection has been cleared because the button is | ||
// being clicked | ||
this.floaterTimeout = window.setTimeout(this.removeFloater.bind(this), 200); | ||
} | ||
} | ||
|
||
addProviderButtons (target: IRangeTarget) { | ||
const btnGroup = this.floatingContainer.firstElementChild; | ||
if (btnGroup) { | ||
btnGroup.innerHTML = ''; | ||
|
||
for (const provider of this.providers) { | ||
const btn = provider.getButton(target); | ||
if (btn) { | ||
btn.addEventListener('click', () => { | ||
this.removeFloater(); | ||
provider.addEnrichment(target); | ||
}); | ||
btnGroup.appendChild(btn); | ||
} | ||
} | ||
} | ||
} | ||
|
||
removeFloater () { | ||
this.floatingContainer.remove(); | ||
this.floaterTimeout = null; | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './popups'; | ||
export * from './gutter'; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can assume that
indigo-akn
will always be used in bootstrap-dependent projects 🤔 and if not we can always style it accordingly🤷🏾There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ja, that's the assumption I'm making. Otherwise we have to style it explicitly here, and we don't have a mechanism for injecting CSS here. :(