Skip to content

Commit

Permalink
#3658 - Move commonly used JS editor code to the JS API
Browse files Browse the repository at this point in the history
- Introduce SCSS defining a set of colors that editors should use for various purposes
- Clean up build of the JS API module
- Added missing exports to the compact_v2 module
- Added markedAnnotations to the AnnotatedText and support filling in when unpacking CompactAnnotatedText
  • Loading branch information
reckart committed Dec 26, 2022
1 parent 56f2de6 commit 1155d4f
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
} from "@inception-project/inception-js-api";
import LabelBadge from "./LabelBadge.svelte";
import SpanText from "./SpanText.svelte";
import { groupRelationsByLabel, groupSpansByLabel, uniqueLabels, uniqueOffsets } from "./Utils";
import { groupRelationsByLabel, groupSpansByLabel, uniqueLabels } from "./Utils";
export let ajaxClient: DiamAjax;
export let data: AnnotatedText;
Expand Down
1 change: 1 addition & 0 deletions inception/inception-js-api/marker-wicket-module
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Marker file which activates the profile "wicket-module" from the parent POM.
7 changes: 7 additions & 0 deletions inception/inception-js-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@
<artifactId>inception-js-api</artifactId>
<name>INCEpTION - JavaScript API</name>
<description>JavaScript APIs for INCEpTION</description>

<dependencies>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.
*/
package de.tudarmstadt.ukp.inception.diam.editor.api.resources;

import org.apache.wicket.request.resource.JavaScriptResourceReference;

public class InceptionJsApiResourceReference
extends JavaScriptResourceReference
{
private static final long serialVersionUID = 1L;

private static final InceptionJsApiResourceReference INSTANCE = //
new InceptionJsApiResourceReference();

/**
* Gets the instance of the resource reference
*
* @return the single instance of the resource reference
*/
public static InceptionJsApiResourceReference get()
{
return INSTANCE;
}

/**
* Private constructor
*/
private InceptionJsApiResourceReference()
{
super(InceptionJsApiResourceReference.class, "InceptionJsAPI.min.js");
}
}
57 changes: 57 additions & 0 deletions inception/inception-js-api/src/main/ts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 yargs from 'yargs/yargs'
import { hideBin } from 'yargs/helpers'
import esbuild from 'esbuild'
import { sassPlugin } from 'esbuild-sass-plugin'
import fs from 'fs-extra'

const argv = yargs(hideBin(process.argv)).argv

const packagePath = 'de/tudarmstadt/ukp/inception/diam/editor/api/resources/'

let outbase = `../../../target/js/${packagePath}`

const defaults = {
bundle: true,
sourcemap: true,
minify: !argv.live,
target: 'es6',
loader: { '.ts': 'ts' },
logLevel: 'info',
plugins: [sassPlugin()]
}

if (argv.live) {
defaults.watch = {
onRebuild (error, result) {
if (error) console.error('watch build failed:', error)
else console.log('watch build succeeded:', result)
}
}
outbase = `../../../target/classes/${packagePath}`
} else {
fs.emptyDirSync(outbase)
}
fs.mkdirsSync(`${outbase}`)

esbuild.build(Object.assign({
entryPoints: ['index.ts'],
outfile: `${outbase}/InceptionJsAPI.min.js`,
globalName: 'InceptionJsAPI'
}, defaults))
2 changes: 0 additions & 2 deletions inception/inception-js-api/src/main/ts/dist/index.js

This file was deleted.

7 changes: 0 additions & 7 deletions inception/inception-js-api/src/main/ts/dist/index.js.map

This file was deleted.

2 changes: 2 additions & 0 deletions inception/inception-js-api/src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import './src/style/InceptionEditorColors.scss'

export * from './src/diam'
export * from './src/editor'
export * from './src/model'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Relation } from './Relation'
import { Span } from './Span'
import { TextMarker } from './TextMarker'
import { Layer } from './Layer'
import { MarkerType } from './Marker'

export class AnnotatedText {
window: Offsets
Expand All @@ -30,6 +31,7 @@ export class AnnotatedText {
relations: Map<VID, Relation> = new Map<VID, Relation>()
spans: Map<VID, Span> = new Map<VID, Span>()
annotationMarkers: Map<VID, AnnotationMarker[]>
markedAnnotations: Map<MarkerType, VID[]>
textMarkers: TextMarker[]

__getOrCreateLayer (id: number): Layer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ export class AnnotationMarker {
type: MarkerType
vid: Array<VID>
}

/**
* Groups the given markers by the VID to which they apply.
*
* @param markerList a list of markers.
* @returns the grouped markers.
*/
export function groupMarkers<T> (markerList: T[] | undefined): Map<VID, Array<T>> {
const markerMap = new Map<VID, Array<T>>()
if (markerList) {
markerList.forEach(marker => {
marker[1].forEach(vid => {
let ms = markerMap.get(vid)
if (!ms) {
ms = []
markerMap.set(vid, ms)
}
ms.push(marker)
})
})
}
return markerMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AnnotationMarker } from '@inception-project/inception-js-api'
import { AnnotatedText, Offsets, VID } from '..'
import { AnnotatedText, Offsets, VID, AnnotationMarker } from '..'
import { MarkerType } from '../Marker'
import { CompactAnnotationMarker, unpackCompactAnnotationMarker } from './CompactAnnotationMarker'
import { CompactLayer, unpackCompactLayer } from './CompactLayer'
import { CompactRelation, unpackCompactRelation } from './CompactRelation'
Expand Down Expand Up @@ -51,18 +51,36 @@ export function unpackCompactAnnotatedText (raw: CompactAnnotatedText): Annotate

const annotationMarkers = raw.annotationMarkers?.map(unpackCompactAnnotationMarker)
cooked.annotationMarkers = makeMarkerMap(annotationMarkers)
cooked.markedAnnotations = makeMarkedAnnotationsMap(annotationMarkers)
cooked.textMarkers = raw.textMarkers?.map(unpackCompactTextMarker) || []
return cooked
}

function makeMarkedAnnotationsMap (annotationMarkers?: AnnotationMarker[]) : Map<MarkerType, VID[]> {
const markedAnnotations = new Map<MarkerType, VID[]>()
if (annotationMarkers) {
for (const marker of annotationMarkers) {
for (const vid of marker.vid) {
let ms = markedAnnotations.get(marker.type)
if (!ms) {
ms = []
markedAnnotations.set(marker.type, ms)
}
ms.push(vid)
}
}
}
return markedAnnotations
}

/**
* Converts a list of {@link CompactAnnotationMarker}s to an easily accessible map using the {@link VID}
* as key and the set of markers on that annotation as values.
*
* @param markerList a list of {@link CompactAnnotationMarker}s
* @returns the map
*/
export function makeMarkerMap<AnnotationMarker> (markerList: AnnotationMarker[] | undefined): Map<VID, Array<AnnotationMarker>> {
export function makeMarkerMap (markerList: AnnotationMarker[] | undefined): Map<VID, Array<AnnotationMarker>> {
const markerMap = new Map<VID, Array<AnnotationMarker>>()
if (markerList) {
markerList.forEach(marker => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export { CompactArgument } from './CompactArgument'
export { CompactRelationAttributes } from './CompactRelationAttributes'
export { CompactSpan } from './CompactSpan'
export { CompactSpanAttributes } from './CompactSpanAttributes'
export { CompactTextMarker } from './CompactTextMarker'
export { CompactAnnotationMarker } from './CompactAnnotationMarker'
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/
$i7n-focus-color: rgba(255,165,0);
$i7n-focus-bounds-color: rgb(255, 106, 0);
$i7n-match-focus-color: rgb(0, 217, 255);

:root {
--i7n-focus-color: #{$i7n-focus-color};
--i7n-focus-bounds-color: #{$i7n-focus-bounds-color};
--i7n-match-focus-color: #{$i7n-match-focus-color};
}
18 changes: 17 additions & 1 deletion inception/inception-js-api/src/main/ts/src/util/Coloring.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@

/*
* 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 type ColorCode = string

// http://24ways.org/2010/calculating-color-contrast/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"url": "https://github.com/inception-project/issues"
},
"scripts": {
"build": "esbuild --bundle index.ts --outdir=dist --minify --sourcemap"
"build": "node build.mjs"
},
"dependencies": {
"@stomp/stompjs": "${stomp-stompjs.version}",
Expand Down

0 comments on commit 1155d4f

Please sign in to comment.