fix: 重新合并 doc-viewer 中的翻译代码(未包含标题翻译)

This commit is contained in:
Zhicheng Wang 2018-03-07 17:16:30 +08:00
parent 0f8d7fd8ce
commit c29aee6729
1 changed files with 104 additions and 65 deletions

View File

@ -1,4 +1,5 @@
import { Component, ComponentRef, DoCheck, ElementRef, EventEmitter, Input, OnDestroy, Output } from '@angular/core'; import { Component, ComponentRef, DoCheck, ElementRef, EventEmitter, Input, OnDestroy, Output } from '@angular/core';
import { HostListener } from '@angular/core';
import { Title, Meta } from '@angular/platform-browser'; import { Title, Meta } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
@ -78,6 +79,7 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
this.hostElement = elementRef.nativeElement; this.hostElement = elementRef.nativeElement;
// Security: the initialDocViewerContent comes from the prerendered DOM and is considered to be secure // Security: the initialDocViewerContent comes from the prerendered DOM and is considered to be secure
this.hostElement.innerHTML = initialDocViewerContent; this.hostElement.innerHTML = initialDocViewerContent;
swapOriginAndResult(this.hostElement);
if (this.hostElement.firstElementChild) { if (this.hostElement.firstElementChild) {
this.currViewContainer = this.hostElement.firstElementChild as HTMLElement; this.currViewContainer = this.hostElement.firstElementChild as HTMLElement;
@ -148,6 +150,7 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
// Security: `doc.contents` is always authored by the documentation team // Security: `doc.contents` is always authored by the documentation team
// and is considered to be safe. // and is considered to be safe.
.do(() => this.nextViewContainer.innerHTML = doc.contents || '') .do(() => this.nextViewContainer.innerHTML = doc.contents || '')
.do(() => swapOriginAndResult(this.nextViewContainer))
.do(() => addTitleAndToc = this.prepareTitleAndToc(this.nextViewContainer, doc.id)) .do(() => addTitleAndToc = this.prepareTitleAndToc(this.nextViewContainer, doc.id))
.switchMap(() => this.embedComponentsService.embedInto(this.nextViewContainer)) .switchMap(() => this.embedComponentsService.embedInto(this.nextViewContainer))
.do(() => this.docReady.emit()) .do(() => this.docReady.emit())
@ -252,4 +255,40 @@ export class DocViewerComponent implements DoCheck, OnDestroy {
this.nextViewContainer.innerHTML = ''; // Empty to release memory. this.nextViewContainer.innerHTML = ''; // Empty to release memory.
}); });
} }
@HostListener('click', ['$event'])
toggleTranslationOrigin($event: MouseEvent): void {
const element = findTranslationResult($event.target as Element);
if (element && element.hasAttribute('translation-result')) {
const origin = element.nextElementSibling;
if (!origin || origin.hasAttribute('translation-result') || origin.tagName !== element.tagName) {
return;
}
if (origin.getAttribute('translation-origin') === 'on') {
origin.setAttribute('translation-origin', 'off');
} else {
origin.setAttribute('translation-origin', 'on');
}
}
}
}
function findTranslationResult(element: Element | null): Element | null {
while (element && !element.hasAttribute('translation-result')) {
element = element.parentElement;
}
return element;
}
function swapOriginAndResult(root: Element): void {
const results = root.querySelectorAll('[translation-result]');
for (let i = 0; i < results.length; ++i) {
const result = results.item(i);
const origin = result.previousElementSibling;
if (origin && origin.hasAttribute('translation-origin') && origin.parentElement) {
origin.parentElement.insertBefore(result, origin);
}
}
} }