Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/elements/ve.ui.PreviewElement.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface PreviewElement class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Creates a ve.ui.PreviewElement object.
10 *
11 * @class
12 * @extends OO.ui.Element
13 * @mixes OO.EventEmitter
14 *
15 * @constructor
16 * @param {ve.dm.Node} [model] Model from which to create a preview
17 * @param {Object} [config] Configuration options
18 * @param {boolean} [config.useView=false] Use the view HTML, and don't bother generating model HTML, which
19 * is a bit slower
20 */
21 ve.ui.PreviewElement = function VeUiPreviewElement( model, config ) {
22 config = config || {};
23
24 // Parent constructor
25 ve.ui.PreviewElement.super.call( this, config );
26
27 // Mixin constructor
28 OO.EventEmitter.call( this );
29
30 this.useView = !!config.useView;
31
32 if ( model ) {
33 this.setModel( model );
34 }
35
36 // Initialize
37 this.$element.addClass( 've-ui-previewElement' );
38 };
39
40 /**
41 * The element rendering has been updated
42 *
43 * @event ve.ui.PreviewElement#render
44 */
45
46 /* Inheritance */
47
48 OO.inheritClass( ve.ui.PreviewElement, OO.ui.Element );
49
50 OO.mixinClass( ve.ui.PreviewElement, OO.EventEmitter );
51
52 /**
53 * Destroy the preview node.
54 */
55 ve.ui.PreviewElement.prototype.destroy = function () {
56 if ( this.view ) {
57 this.view.destroy();
58 this.view = null;
59 }
60 };
61
62 /**
63 * Set the model node for the preview
64 *
65 * @param {ve.dm.Node} model Model from which to create a preview
66 */
67 ve.ui.PreviewElement.prototype.setModel = function ( model ) {
68 this.model = model;
69 this.updatePreview();
70 };
71
72 /**
73 * Modify DOM node before appending to the preview
74 *
75 * @param {HTMLElement} element Element to be appended
76 */
77 ve.ui.PreviewElement.prototype.beforeAppend = function ( element ) {
78 // Remove slugs and nails. This used to be done in CSS but triggered
79 // a catastrophic browser bug in Chrome (T341901)
80 Array.prototype.forEach.call( element.querySelectorAll( '.ve-ce-nail, .ve-ce-branchNode-slug' ), ( el ) => {
81 el.parentNode.removeChild( el );
82 } );
83 ve.targetLinksToNewWindow( element );
84 };
85
86 /**
87 * Replace the content of the body with the model DOM
88 *
89 * Doesn't use jQuery to avoid document switching performance bug
90 */
91 ve.ui.PreviewElement.prototype.replaceWithModelDom = function () {
92 const htmlDocument = ve.dm.converter.getDomFromNode( this.model, ve.dm.Converter.static.PREVIEW_MODE ),
93 body = htmlDocument.body,
94 element = this.$element[ 0 ];
95
96 // Resolve attributes (in particular, expand 'href' and 'src' using the right base)
97 ve.resolveAttributes(
98 body,
99 this.model.getDocument().getHtmlDocument(),
100 ve.dm.Converter.static.computedAttributes
101 );
102
103 this.beforeAppend( body );
104
105 // Move content to element
106 element.innerHTML = '';
107 while ( body.childNodes.length ) {
108 element.appendChild(
109 element.ownerDocument.adoptNode( body.childNodes[ 0 ] )
110 );
111 }
112
113 this.afterRender();
114 };
115
116 /**
117 * Update the preview
118 */
119 ve.ui.PreviewElement.prototype.updatePreview = function () {
120 // Initial CE node
121 this.view = ve.ce.nodeFactory.createFromModel( this.model );
122 this.beforeAppend( this.view.$element[ 0 ] );
123 this.$element.append( this.view.$element );
124 this.view.setLive( true );
125
126 ve.ce.GeneratedContentNode.static.awaitGeneratedContent( this.view )
127 .then( () => {
128 // When all children are rerendered, replace with DM DOM for a better preview.
129 // Conversion should be pretty fast, but avoid this (by setting useView to true)
130 // if you generating a lot of previews, e.g. in a list
131 if ( !this.useView ) {
132 // Verify that the PreviewElement hasn't been destroyed.
133 if ( this.view ) {
134 this.replaceWithModelDom();
135 }
136 } else {
137 this.afterRender();
138 }
139 } );
140 };
141
142 /**
143 * Cleanup and emit events after render
144 *
145 * @fires ve.ui.PreviewElement#render
146 */
147 ve.ui.PreviewElement.prototype.afterRender = function () {
148 // Cleanup
149 this.view.destroy();
150 this.view = null;
151
152 // Event
153 this.emit( 'render' );
154 };
155
156 /**
157 * Check if the preview is still generating
158 *
159 * @return {boolean} Still generating
160 */
161 ve.ui.PreviewElement.prototype.isGenerating = function () {
162 return !!this.view;
163 };