diff --git a/app/components/embed/m3_component.html.erb b/app/components/embed/m3_component.html.erb index 576076b49..39ffe10ed 100644 --- a/app/components/embed/m3_component.html.erb +++ b/app/components/embed/m3_component.html.erb @@ -4,6 +4,7 @@ id='sul-embed-m3' class='m3' data-m3-uri='<%= viewer.manifest_json_url %>' + data-viewer-config='<%= viewer.iiif_initial_viewer_config %>' data-canvas-id='<%= viewer.canvas_id %>' data-canvas-index='<%= viewer.canvas_index %>' data-hide-title='<%= viewer.embed_request.hide_title? %>' diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rb index b926612cb..6f4a0286b 100644 --- a/app/controllers/embed_controller.rb +++ b/app/controllers/embed_controller.rb @@ -55,7 +55,7 @@ def linted_params raise ActionController::BadRequest end params.permit(:url, :maxwidth, :maxheight, :format, :fullheight, :new_component, - :hide_title, :hide_embed, :hide_download, :hide_search, :min_files_to_search, + :hide_title, :hide_embed, :hide_download, :hide_search, :min_files_to_search, :iiif_initial_viewer_config, :canvas_id, :canvas_index, :search, :suggested_search, :image_tools, :cdl_hold_record_id) end diff --git a/app/javascript/src/modules/m3_viewer.js b/app/javascript/src/modules/m3_viewer.js index a1d553e28..5614742f1 100644 --- a/app/javascript/src/modules/m3_viewer.js +++ b/app/javascript/src/modules/m3_viewer.js @@ -12,6 +12,7 @@ import embedModePlugin from '../plugins/embedModePlugin'; import analyticsPlugin from '../plugins/analyticsPlugin'; import cdlAuthPlugin from '../plugins/cdlAuthPlugin'; import xywhPlugin from '../plugins/xywhPlugin'; +import { getExportableState } from 'mirador/dist/es/src/state/selectors'; export default { init: function() { @@ -31,7 +32,10 @@ export default { sideBarPanel = 'attribution'; } - Mirador.viewer({ + let viewerConfig; + if (data.viewerConfig) { viewerConfig = JSON.parse(data.viewerConfig) } + + const viewerInstance = Mirador.viewer({ id: 'sul-embed-m3', miradorDownloadPlugin: { restrictDownloadOnSizeDefinition: true, @@ -81,6 +85,7 @@ export default { suggestedSearches: data.suggestedSearch.length > 0 ? [data.suggestedSearch] : null, loadedManifest: data.m3Uri, canvasIndex: Number(data.canvasIndex), + initialViewerConfig: viewerConfig, canvasId: data.canvasId, ...(cdl && { cdl: { @@ -136,5 +141,38 @@ export default { analyticsPlugin, xywhPlugin, ].filter(Boolean)); + + window.addEventListener('message', (event) => { + const regex = /^(stanford\.edu|[\w-]+\.stanford\.edu)$/; + if (regex.exec(event.origin) === null) { + return; + } + + if (event && event.data) { + let parsedData; + try { + parsedData = typeof event.data === 'string' ? JSON.parse(event.data) : event.data; + } catch (error) { + console.error('Failed to parse event data:', error); + return; // Exit if parsing fails + } + + if (parsedData.type === "requestState") { + const currentState = viewerInstance.store.getState(); + const exportableState = getExportableState(currentState); + const xywhCoords = viewerInstance.container.querySelector('[data-xywh-coords]').dataset.xywhCoords; + + // Send the state back to the parent window + window.parent.postMessage( + JSON.stringify({ + type: 'stateResponse', + data: {...exportableState, ...{'xywh': xywhCoords}}, + source: 'sul-embed-m3', + }), + event.origin + ); + } + } + }); } }; diff --git a/app/viewers/embed/viewer/m3_viewer.rb b/app/viewers/embed/viewer/m3_viewer.rb index fb541ed02..b6029f1b6 100644 --- a/app/viewers/embed/viewer/m3_viewer.rb +++ b/app/viewers/embed/viewer/m3_viewer.rb @@ -3,7 +3,7 @@ module Embed module Viewer class M3Viewer < CommonViewer - delegate :search, :suggested_search, :canvas_id, :cdl_hold_record_id, to: :embed_request + delegate :search, :suggested_search, :canvas_id, :cdl_hold_record_id, :iiif_initial_viewer_config, to: :embed_request def component M3Component diff --git a/app/views/embed/iiif.html.erb b/app/views/embed/iiif.html.erb index 196fb710e..6b8587e52 100644 --- a/app/views/embed/iiif.html.erb +++ b/app/views/embed/iiif.html.erb @@ -32,6 +32,7 @@ data-search='<%= @embed_request.search %>' data-suggested-search='<%= @embed_request.suggested_search %>' data-image-tools='<%= @embed_request.image_tools %>' + data-viewer-config='<%= @embed_request.iiif_initial_viewer_config %>' style='height:100%; width:100%'> diff --git a/lib/embed/request.rb b/lib/embed/request.rb index f80a22c0e..cf5fbaa8f 100644 --- a/lib/embed/request.rb +++ b/lib/embed/request.rb @@ -77,7 +77,8 @@ def as_url_params :min_files_to_search, :canvas_id, :canvas_index, :search, :suggested_search, - :image_tools + :image_tools, + :iiif_initial_viewer_config ) if p.respond_to? :permit! @@ -107,6 +108,10 @@ def image_tools params[:image_tools] end + def iiif_initial_viewer_config + params[:iiif_initial_viewer_config] + end + def validate!(url_parameter: true, url_scheme: true, format: true) require_url_parameter if url_parameter validate_url_scheme if url_scheme