|
@@ -9,7 +9,7 @@ import {
|
|
|
|
|
|
import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils';
|
|
|
|
|
|
-import { IDocumentWidget } from '@jupyterlab/docregistry';
|
|
|
+import { IDocumentWidget, DocumentRegistry } from '@jupyterlab/docregistry';
|
|
|
|
|
|
import {
|
|
|
ImageViewer,
|
|
@@ -41,13 +41,28 @@ namespace CommandIDs {
|
|
|
/**
|
|
|
* The list of file types for images.
|
|
|
*/
|
|
|
-const FILE_TYPES = ['png', 'gif', 'jpeg', 'svg', 'bmp', 'ico', 'xbm', 'tiff'];
|
|
|
+const FILE_TYPES = ['png', 'gif', 'jpeg', 'bmp', 'ico', 'tiff'];
|
|
|
|
|
|
/**
|
|
|
* The name of the factory that creates image widgets.
|
|
|
*/
|
|
|
const FACTORY = 'Image';
|
|
|
|
|
|
+/**
|
|
|
+ * The name of the factory that creates image widgets.
|
|
|
+ */
|
|
|
+const TEXT_FACTORY = 'Image (Text)';
|
|
|
+
|
|
|
+/**
|
|
|
+ * The list of file types for images with optional text modes.
|
|
|
+ */
|
|
|
+const TEXT_FILE_TYPES = ['svg', 'xbm'];
|
|
|
+
|
|
|
+/**
|
|
|
+ * The test pattern for text file types in paths.
|
|
|
+ */
|
|
|
+const TEXT_FILE_REGEX = new RegExp(`\.(${TEXT_FILE_TYPES.join('|')})$`);
|
|
|
+
|
|
|
/**
|
|
|
* The image file handler extension.
|
|
|
*/
|
|
@@ -73,13 +88,47 @@ function activate(
|
|
|
restorer: ILayoutRestorer | null
|
|
|
): IImageTracker {
|
|
|
const namespace = 'image-widget';
|
|
|
+
|
|
|
+ function onWidgetCreated(
|
|
|
+ sender: any,
|
|
|
+ widget: IDocumentWidget<ImageViewer, DocumentRegistry.IModel>
|
|
|
+ ) {
|
|
|
+ // Notify the widget tracker if restore data needs to update.
|
|
|
+ widget.context.pathChanged.connect(() => {
|
|
|
+ void tracker.save(widget);
|
|
|
+ });
|
|
|
+ void tracker.add(widget);
|
|
|
+
|
|
|
+ const types = app.docRegistry.getFileTypesForPath(widget.context.path);
|
|
|
+
|
|
|
+ if (types.length > 0) {
|
|
|
+ widget.title.icon = types[0].icon!;
|
|
|
+ widget.title.iconClass = types[0].iconClass ?? '';
|
|
|
+ widget.title.iconLabel = types[0].iconLabel ?? '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
const factory = new ImageViewerFactory({
|
|
|
name: FACTORY,
|
|
|
modelName: 'base64',
|
|
|
- fileTypes: FILE_TYPES,
|
|
|
+ fileTypes: [...FILE_TYPES, ...TEXT_FILE_TYPES],
|
|
|
defaultFor: FILE_TYPES,
|
|
|
readOnly: true
|
|
|
});
|
|
|
+
|
|
|
+ const textFactory = new ImageViewerFactory({
|
|
|
+ name: TEXT_FACTORY,
|
|
|
+ modelName: 'text',
|
|
|
+ fileTypes: TEXT_FILE_TYPES,
|
|
|
+ defaultFor: TEXT_FILE_TYPES,
|
|
|
+ readOnly: true
|
|
|
+ });
|
|
|
+
|
|
|
+ [factory, textFactory].forEach(factory => {
|
|
|
+ app.docRegistry.addWidgetFactory(factory);
|
|
|
+ factory.widgetCreated.connect(onWidgetCreated);
|
|
|
+ });
|
|
|
+
|
|
|
const tracker = new WidgetTracker<IDocumentWidget<ImageViewer>>({
|
|
|
namespace
|
|
|
});
|
|
@@ -88,29 +137,16 @@ function activate(
|
|
|
// Handle state restoration.
|
|
|
void restorer.restore(tracker, {
|
|
|
command: 'docmanager:open',
|
|
|
- args: widget => ({ path: widget.context.path, factory: FACTORY }),
|
|
|
+ args: widget => ({
|
|
|
+ path: widget.context.path,
|
|
|
+ factory: TEXT_FILE_REGEX.test(widget.context.path)
|
|
|
+ ? TEXT_FACTORY
|
|
|
+ : FACTORY
|
|
|
+ }),
|
|
|
name: widget => widget.context.path
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- app.docRegistry.addWidgetFactory(factory);
|
|
|
-
|
|
|
- factory.widgetCreated.connect((sender, widget) => {
|
|
|
- // Notify the widget tracker if restore data needs to update.
|
|
|
- widget.context.pathChanged.connect(() => {
|
|
|
- void tracker.save(widget);
|
|
|
- });
|
|
|
- void tracker.add(widget);
|
|
|
-
|
|
|
- const types = app.docRegistry.getFileTypesForPath(widget.context.path);
|
|
|
-
|
|
|
- if (types.length > 0) {
|
|
|
- widget.title.icon = types[0].icon!;
|
|
|
- widget.title.iconClass = types[0].iconClass ?? '';
|
|
|
- widget.title.iconLabel = types[0].iconLabel ?? '';
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
addCommands(app, tracker);
|
|
|
|
|
|
if (palette) {
|