Skip to content

Commit

Permalink
Issue #4139: Reusable popover component for annotation editors
Browse files Browse the repository at this point in the history
- Basic Svelte component for info popover
- Use this component in the Apache Annotator editor
  • Loading branch information
reckart committed Aug 3, 2023
1 parent 8bf9746 commit 15fb450
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package de.tudarmstadt.ukp.inception.rendering;

import static de.tudarmstadt.ukp.clarin.webanno.model.MultiValueMode.NONE;
import static de.tudarmstadt.ukp.clarin.webanno.support.uima.ICasUtil.getAddr;
import static de.tudarmstadt.ukp.clarin.webanno.support.uima.ICasUtil.selectByAddr;
import static de.tudarmstadt.ukp.inception.schema.validation.ValidationUtils.isRequiredFeatureMissing;
import static org.apache.commons.lang3.StringUtils.defaultString;
Expand Down Expand Up @@ -135,7 +134,7 @@ default void renderRequiredFeatureErrors(List<AnnotationFeature> aFeatures,
}

if (isRequiredFeatureMissing(f, aFS)) {
aResponse.add(new VComment(new VID(getAddr(aFS)), VCommentType.ERROR,
aResponse.add(new VComment(VID.of(aFS), VCommentType.ERROR,
"Required feature [" + f.getName() + "] not set."));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,9 @@
class="text-secondary bg-light-subtle border-end px-2 d-flex align-items-center"
>
{#if ann instanceof Span}
<div class="annotation-type-marker">
</div>
<div class="annotation-type-marker i7n-icon-span"/>
{:else if ann instanceof Relation}
<div class="annotation-type-marker">
</div>
<div class="annotation-type-marker i7n-icon-relation"/>
{/if}
</div>
<!-- svelte-ignore a11y-click-events-have-key-events -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@
</div>

<style>
@import '../node_modules/@inception-project/inception-js-api/src/style/InceptionEditorIcons.scss';
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<!--
Licensed to the Technische Universität Darmstadt under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The Technische Universität Darmstadt
licenses this file to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<svelte:options accessors={true}/>

<script lang="ts">
import { Annotation, AnnotationOverEvent, Relation, Span, DiamAjax } from '@inception-project/inception-js-api'
import { onMount } from 'svelte'
export let ajax : DiamAjax
export let root : Element
export let top : number = 0
export let left : number = 0
export let width : number = 400
export let annotation : Annotation | undefined = undefined
let popover : HTMLElement
onMount(() => {
// React to mouse hovering over annotation
root.addEventListener(AnnotationOverEvent.eventType, (e: AnnotationOverEvent) => {
if (!(e.originalEvent instanceof MouseEvent) || !(e.target instanceof HTMLElement)) return
annotation = e.annotation
})
// Follow the mouse around
root.addEventListener('mousemove', (e: MouseEvent) => {
if (!annotation) return
const rect = popover.getBoundingClientRect()
const x = e.clientX
const y = e.clientY + 16
// Flip up if the popover is about to be clipped at the bottom
if (y + rect.height > window.innerHeight) {
top = y - rect.height
}
else {
top = y
}
// Shift left if the popover is about to be clipped on the right
if (x + rect.width > window.innerWidth) {
left = Math.max(0, window.innerWidth - rect.width)
}
else {
left = x
}
})
// Hide popover when leaving the annotation
root.addEventListener('mouseout', e => {
if (annotation) annotation = undefined
})
// $: {
// if (annotation) {
// ajax.loadLazyDetails(annotation.vid)
// }
// }
})
</script>

<div bind:this={popover} class="bootstrap popover position-fixed shadow" style:top="{top}px" style:left="{left}px" style:--width="{width}px" class:d-none={!annotation}>
<div class="popover-header p-0 d-flex">
<div class="border-end border-secondary px-1">
<span class="annotation-type-marker"
class:i7n-icon-span={annotation instanceof Span}
class:i7n-icon-relation={annotation instanceof Relation}/>
</div>
<div class="flex-grow-1 px-1">{annotation?.layer?.name}</div>
<div class="text-body-secondary px-1">ID: {annotation?.vid}</div>
</div>
{#if annotation}
<div class="popover-body p-1">
{#each annotation.comments as comment}
<div class="i7n-marker-{comment.type}">{comment.comment}</div>
{/each}
</div>
{/if}
</div>

<!-- svelte-ignore css-unused-selector -->
<style lang="scss">
@import '../../node_modules/bootstrap/scss/bootstrap.scss';
@import '../../node_modules/@inception-project/inception-js-api/src/style/InceptionEditorIcons.scss';
@import '../../node_modules/@inception-project/inception-js-api/src/style/InceptionEditorColors.scss';
.bootstrap {
// Ensure that Bootstrap properly applies to the component
@extend body
}
.popover {
min-width: var(--width);
width: var(--width);
max-width: var(--width);
pointer-events: none;
}
.annotation-type-marker {
width: 1em;
display: inline-block;
text-align: center;
}
.i7n-marker-error {
color: var(--i7n-error-color);
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
export let highlight: HTMLElement = undefined
export let position: 'begin' | 'end'
export let handle: HTMLElement
export let marker: HTMLElement
export let handle: HTMLElement = undefined
export let marker: HTMLElement = undefined
export let dragging = false
let borderRadius = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/
@import '~@inception-project/inception-js-api/src/style/InceptionEditorColors.scss';
@import '~@inception-project/inception-js-api/src/style/InceptionEditorIcons.scss';

:root {
--hover-brightness: 1.2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import { highlights, ApacheAnnotatorVisualizer } from './ApacheAnnotatorVisualiz
import { ApacheAnnotatorSelector } from './ApacheAnnotatorSelector'
import ApacheAnnotatorToolbar from './ApacheAnnotatorToolbar.svelte'
import { showEmptyHighlights, showLabels } from './ApacheAnnotatorState'
import AnnotationDetailPopOver from './AnnotationDetailPopOver.svelte'

export class ApacheAnnotatorEditor implements AnnotationEditor {
private ajax: DiamAjax
private root: Element
private vis: ApacheAnnotatorVisualizer
private selector: ApacheAnnotatorSelector
private toolbar: ApacheAnnotatorToolbar
private popover: AnnotationDetailPopOver

public constructor (element: Element, ajax: DiamAjax, userPreferencesKey: string) {
this.ajax = ajax
Expand Down Expand Up @@ -68,6 +70,14 @@ export class ApacheAnnotatorEditor implements AnnotationEditor {
this.selector = new ApacheAnnotatorSelector(this.root, this.ajax)
this.toolbar = this.createToolbar()

this.popover = new AnnotationDetailPopOver({
target: this.root.ownerDocument.body,
props: {
root: this.root,
ajax: this.ajax
}
})

// Event handler for creating an annotion or selecting an annotation
this.root.addEventListener('mouseup', e => this.onMouseUp(e))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/
import './ApacheAnnotatorEditor.scss'
import { unpackCompactAnnotatedTextV2, DiamAjax, DiamLoadAnnotationsOptions, VID, ViewportTracker, offsetToRange, AnnotatedText, Span, TextMarker, Offsets } from '@inception-project/inception-js-api'
import { unpackCompactAnnotatedTextV2, DiamAjax, DiamLoadAnnotationsOptions, VID, ViewportTracker, offsetToRange, AnnotatedText, Span, TextMarker, Offsets, AnnotationOverEvent, AnnotationOutEvent, Annotation } from '@inception-project/inception-js-api'
import { CompactAnnotatedText } from '@inception-project/inception-js-api/src/model/compact_v2'
import { highlightText } from '@apache-annotator/dom'
import { showEmptyHighlights, showLabels } from './ApacheAnnotatorState'
Expand All @@ -36,10 +36,14 @@ export class ApacheAnnotatorVisualizer {
private showInlineLabels = false
private showEmptyHighlights = false

private data? : AnnotatedText

private removePingMarkers: (() => void)[] = []
private removePingMarkersTimeout: number | undefined = undefined
private alpha = '55'

private overAnnotation : Annotation | undefined

constructor (element: Element, ajax: DiamAjax) {
this.ajax = ajax
this.root = element
Expand All @@ -50,6 +54,17 @@ export class ApacheAnnotatorVisualizer {
// Event handlers for the resizer component
this.root.addEventListener('mouseover', e => this.showResizer(e))

// Event handlers for custom events
this.root.addEventListener('mouseover', event => {
if (!(event instanceof MouseEvent) || !(event.target instanceof HTMLElement)) return
const vid = event.target.getAttribute('data-iaa-id')
if (!vid) return
const annotation = this.data?.getAnnotation(vid)
if (!annotation) return
this.overAnnotation = annotation
event.target.dispatchEvent(new AnnotationOverEvent(annotation, event))
})

// Add event handlers for highlighting extent of the annotation the mouse is currently over
this.root.addEventListener('mouseover', e => this.addAnnotationHighlight(e as MouseEvent))
this.root.addEventListener('mouseout', e => this.removeAnnotationHighight(e as MouseEvent))
Expand Down Expand Up @@ -98,7 +113,10 @@ export class ApacheAnnotatorVisualizer {
console.log(`Loading annotations for range ${JSON.stringify(options.range)}`)

this.ajax.loadAnnotations(options)
.then((doc: CompactAnnotatedText) => this.renderAnnotations(unpackCompactAnnotatedTextV2(doc)))
.then((doc: CompactAnnotatedText) => {
this.data = unpackCompactAnnotatedTextV2(doc)
this.renderAnnotations(this.data)
})
}

private renderAnnotations (doc: AnnotatedText): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,18 @@ export class ResizeManager {

this.visualizer.root.addEventListener('dragover', e => this.handleDragOver(e))

// @ts-expect-error - VSCode does not seem to understand the Svelte component
this.beginHandle = new AnnotationResizeHandle({
target: this.visualizer.root,
props: { position: 'begin' }
})
// @ts-expect-error - VSCode does not seem to understand the Svelte component

this.endHandle = new AnnotationResizeHandle({
target: this.visualizer.root,
props: { position: 'end' }
})

// @ts-expect-error - VSCode does not seem to understand the Svelte component
this.beginHandle.$on('resize-handle-released', e => this.handleResizeHandleReleased(e))

// @ts-expect-error - VSCode does not seem to understand the Svelte component
this.endHandle.$on('resize-handle-released', e => this.handleResizeHandleReleased(e))

this.hide()
Expand All @@ -65,8 +62,8 @@ export class ResizeManager {

const highlights = Array.from(this.visualizer.getHighlightsForAnnotation(id))
if (!highlights.length) return
this.beginHandle.highlight = highlights[0]
this.endHandle.highlight = highlights[highlights.length - 1]
this.beginHandle.highlight = highlights[0] as HTMLElement
this.endHandle.highlight = highlights[highlights.length - 1] as HTMLElement

this.mouseOverHandler = e => this.handleMouseOver(e)
this.visualizer.root.addEventListener('mouseover', this.mouseOverHandler)
Expand Down
1 change: 1 addition & 0 deletions inception/inception-js-api/src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './src/style/InceptionEditorColors.scss'
export * from './src/diam'
export * from './src/editor'
export * from './src/model'
export * from './src/event'
export * from './src/model/compact'
export * from './src/util'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Technische Universität Darmstadt under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Technische Universität Darmstadt
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Annotation } from '../model'

const eventType = 'i7n-annotation-over'
export class AnnotationOverEvent extends Event {
static eventType = eventType

originalEvent: Event
annotation: Annotation

constructor (annotation: Annotation, originalEvent: Event) {
super(eventType, { bubbles: true })
this.originalEvent = originalEvent
this.annotation = annotation
}
}
18 changes: 18 additions & 0 deletions inception/inception-js-api/src/main/ts/src/event/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Licensed to the Technische Universität Darmstadt under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Technische Universität Darmstadt
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { AnnotationOverEvent } from './AnnotationOverEvent'
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Span } from './Span'
import { TextMarker } from './TextMarker'
import { Layer } from './Layer'
import { MarkerType } from './Marker'
import { Annotation } from './Annotation'

export class AnnotatedText {
window: Offsets
Expand Down Expand Up @@ -56,4 +57,8 @@ export class AnnotatedText {
this.relations.set(annotation.vid, annotation)
}
}

public getAnnotation (id: VID): Annotation | undefined {
return this.spans.get(id) || this.relations.get(id)
}
}
Loading

0 comments on commit 15fb450

Please sign in to comment.