MediaWiki:Gadget-FDVEDiagramLink.js: Difference between revisions

Remove the dead "Change which diagram is shown" link: the transclusion dialog is empty for a parser function, so it was a false affordance
Recognise {{Diagram|page=}} as well as the bare parser function, so converted embeds keep double-click-to-edit
Line 65: Line 65:
* upgrade reverts that patch.
* upgrade reverts that patch.
*
*
target.wt       "#display_diagram:Mermaid:Some_Page"
* Two shapes are recognised:
target.function "display_diagram"
{{Diagram|page=Mermaid:Foo}}    -> params.page.wt       (VE-editable)
*  {{#display_diagram:Mermaid:Foo}} -> target.wt after ':'  (legacy, still valid)
*
*
* @param {ve.dm.MWTransclusionNode} model
* @param {ve.dm.MWTransclusionNode} model
Line 72: Line 73:
*/
*/
function diagramPageFromModel( model ) {
function diagramPageFromModel( model ) {
var mwData, parts, i, target, wt, colon;
var mwData, parts, i, part, target, params, wt, colon;


try {
try {
Line 85: Line 86:


for ( i = 0; i < parts.length; i++ ) {
for ( i = 0; i < parts.length; i++ ) {
target = parts[ i ] && parts[ i ].template && parts[ i ].template.target;
part = parts[ i ] && parts[ i ].template;
if ( !target || target.function !== 'display_diagram' ) {
target = part && part.target;
if ( !target ) {
continue;
continue;
}
}
wt = String( target.wt || '' );
 
colon = wt.indexOf( ':' );
// Shape 1 - {{Diagram|page=Mermaid:Foo}}. Parsoid gives a template part
if ( colon === -1 ) {
// a target.href and NO target.function, putting the page name in
// params.page. This is the shape VE can actually edit - parameters are
// editable, a parser-function target is not - which is the whole reason
// Template:Diagram exists.
if ( normaliseTemplateName( target ) === WRAPPER_TEMPLATE ) {
params = part.params;
wt = params && params.page && params.page.wt;
wt = ( wt === undefined || wt === null ) ? '' : String( wt ).trim();
if ( wt ) {
return wt;
}
continue;
continue;
}
}
// Everything after "#display_diagram:" is the diagram page, which
 
// itself contains a namespace colon.
// Shape 2 - a bare {{#display_diagram:Mermaid:Foo}}. Still supported:
wt = wt.slice( colon + 1 ).trim();
// embeds predating the template use it, and it remains legal wikitext.
if ( wt ) {
if ( target.function === 'display_diagram' ) {
return wt;
wt = String( target.wt || '' );
colon = wt.indexOf( ':' );
if ( colon === -1 ) {
continue;
}
// Everything after "#display_diagram:" is the diagram page, which
// itself contains a namespace colon.
wt = wt.slice( colon + 1 ).trim();
if ( wt ) {
return wt;
}
}
}
}
}
return null;
return null;
}
// Name of the wrapper template, normalised (see normaliseTemplateName).
var WRAPPER_TEMPLATE = 'diagram';
/**
* Reduce a transclusion target to a bare, comparable template name.
* Parsoid hrefs look like "./Template:Diagram"; hand-written wikitext may say
* "Template:Diagram", "diagram" or use underscores.
*
* @param {Object} target
* @return {string} lower-cased name with no namespace prefix
*/
function normaliseTemplateName( target ) {
return String( ( target && ( target.href || target.wt ) ) || '' )
.replace( /^\.\//, '' )
.replace( /^Template:/i, '' )
.replace( /_/g, ' ' )
.trim()
.toLowerCase();
}
}