Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
3b76b471
V
vscode
项目概览
掘金者说
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3b76b471
编写于
1月 15, 2016
作者:
J
Johannes Rieken
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
listen to model changes and iframe reparenting
上级
f1e652d0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
88 addition
and
38 deletion
+88
-38
src/vs/base/common/lifecycle.ts
src/vs/base/common/lifecycle.ts
+5
-4
src/vs/workbench/parts/html/browser/htmlPreviewPart.ts
src/vs/workbench/parts/html/browser/htmlPreviewPart.ts
+78
-34
src/vs/workbench/parts/html/common/htmlInput.ts
src/vs/workbench/parts/html/common/htmlInput.ts
+5
-0
未找到文件。
src/vs/base/common/lifecycle.ts
浏览文件 @
3b76b471
...
...
@@ -13,12 +13,13 @@ export interface IDisposable {
}
export
function
disposeAll
<
T
extends
IDisposable
>
(
arr
:
T
[]):
T
[]
{
for
(
let
i
=
0
,
len
=
arr
.
length
;
i
<
len
;
i
++
)
{
if
(
arr
[
i
])
{
arr
[
i
].
dispose
();
if
(
arr
)
{
for
(
let
i
=
0
,
len
=
arr
.
length
;
i
<
len
;
i
++
)
{
if
(
arr
[
i
])
{
arr
[
i
].
dispose
();
}
}
}
return
[];
}
...
...
src/vs/workbench/parts/html/browser/htmlPreviewPart.ts
浏览文件 @
3b76b471
...
...
@@ -8,15 +8,11 @@
// import 'vs/css!./media/iframeeditor';
import
{
localize
}
from
'
vs/nls
'
;
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
import
{
IModel
}
from
'
vs/editor/common/editorCommon
'
;
import
URI
from
'
vs/base/common/uri
'
;
import
{
IModel
,
EventType
}
from
'
vs/editor/common/editorCommon
'
;
import
{
Dimension
,
Builder
}
from
'
vs/base/browser/builder
'
;
import
*
as
DOM
from
'
vs/base/browser/dom
'
;
import
*
as
errors
from
'
vs/base/common/errors
'
;
import
{
cAll
}
from
'
vs/base/common/lifecycle
'
;
import
{
EditorOptions
,
EditorInput
}
from
'
vs/workbench/common/editor
'
;
import
{
EditorInputAction
,
BaseEditor
}
from
'
vs/workbench/browser/parts/editor/baseEditor
'
;
import
{
IFrameEditorInput
}
from
'
vs/workbench/common/editor/iframeEditorInput
'
;
import
{
IFrameEditorModel
}
from
'
vs/workbench/common/editor/iframeEditorModel
'
;
import
{
BaseEditor
}
from
'
vs/workbench/browser/parts/editor/baseEditor
'
;
import
{
Position
}
from
'
vs/platform/editor/common/editor
'
;
import
{
ITelemetryService
}
from
'
vs/platform/telemetry/common/telemetry
'
;
import
{
IWorkbenchEditorService
}
from
'
vs/workbench/services/editor/common/editorService
'
;
...
...
@@ -33,6 +29,10 @@ export class HtmlPreviewPart extends BaseEditor {
private
_iFrameElement
:
HTMLIFrameElement
;
private
_editorService
:
IWorkbenchEditorService
;
private
_model
:
IModel
;
private
_modelChangeUnbind
:
Function
;
private
_lastModelVersion
:
number
;
constructor
(
@
ITelemetryService
telemetryService
:
ITelemetryService
,
@
IWorkbenchEditorService
editorService
:
IWorkbenchEditorService
...
...
@@ -46,6 +46,10 @@ export class HtmlPreviewPart extends BaseEditor {
// remove from dome
const
element
=
this
.
_iFrameElement
.
parentElement
;
element
.
parentElement
.
removeChild
(
element
);
// unhook from model
this
.
_modelChangeUnbind
=
cAll
(
this
.
_modelChangeUnbind
);
this
.
_model
=
undefined
;
}
public
createEditor
(
parent
:
Builder
):
void
{
...
...
@@ -87,46 +91,92 @@ export class HtmlPreviewPart extends BaseEditor {
return
this
.
input
.
getName
();
}
public
setVisible
(
visible
:
boolean
,
position
?:
Position
):
TPromise
<
void
>
{
return
super
.
setVisible
(
visible
,
position
).
then
(()
=>
{
if
(
visible
&&
this
.
_model
)
{
this
.
_modelChangeUnbind
=
this
.
_model
.
addListener
(
EventType
.
ModelContentChanged2
,
()
=>
this
.
_updateIFrameContent
());
this
.
_updateIFrameContent
();
}
else
{
this
.
_modelChangeUnbind
=
cAll
(
this
.
_modelChangeUnbind
);
}
})
}
public
changePosition
(
position
:
Position
):
void
{
super
.
changePosition
(
position
);
// reparenting an IFRAME into another DOM element yields weird results when the contents are made
// of a string and not a URL. to be on the safe side we reload the iframe when the position changes
// and we do it using a timeout of 0 to reload only after the position has been changed in the DOM
setTimeout
(()
=>
{
this
.
_updateIFrameContent
(
true
);
},
0
);
}
public
setInput
(
input
:
EditorInput
,
options
:
EditorOptions
):
TPromise
<
void
>
{
this
.
_model
=
undefined
;
this
.
_modelChangeUnbind
=
cAll
(
this
.
_modelChangeUnbind
);
this
.
_lastModelVersion
=
-
1
;
if
(
!
(
input
instanceof
HtmlInput
))
{
return
TPromise
.
wrapError
<
void
>
(
'
Invalid input
'
);
}
return
this
.
_editorService
.
resolveEditorModel
(
input
).
then
(
model
=>
{
let
textModel
:
IModel
;
if
(
model
instanceof
ResourceEditorModel
)
{
t
extM
odel
=
model
.
textEditorModel
t
his
.
_m
odel
=
model
.
textEditorModel
}
if
(
!
t
extM
odel
)
{
if
(
!
t
his
.
_m
odel
)
{
return
TPromise
.
wrapError
<
void
>
(
localize
(
'
html.voidInput
'
,
"
Invalid editor input.
"
));
}
let
parser
=
new
DOMParser
();
let
newDocument
=
parser
.
parseFromString
(
textModel
.
getValue
(),
'
text/html
'
);
// newDocument.body.appendChild(KeybindingEnabler.script());
let
iFrameDocument
=
this
.
_iFrameElement
.
contentWindow
.
document
;
if
((
<
HTMLElement
>
iFrameDocument
.
firstChild
).
innerHTML
===
'
<head></head><body></body>
'
)
{
iFrameDocument
.
open
(
'
text/html
'
,
'
replace
'
);
iFrameDocument
.
write
(
KeybindingEnabler
.
defaultHtml
());
iFrameDocument
.
close
();
}
if
(
newDocument
.
head
.
innerHTML
!==
iFrameDocument
.
head
.
innerHTML
)
{
iFrameDocument
.
head
.
innerHTML
=
newDocument
.
head
.
innerHTML
;
}
if
(
newDocument
.
body
.
innerHTML
!==
iFrameDocument
.
body
.
innerHTML
)
{
iFrameDocument
.
body
.
innerHTML
=
newDocument
.
body
.
innerHTML
;
}
this
.
_modelChangeUnbind
=
this
.
_model
.
addListener
(
EventType
.
ModelContentChanged2
,
()
=>
this
.
_updateIFrameContent
());
this
.
_updateIFrameContent
();
return
super
.
setInput
(
input
,
options
);
});
}
private
_updateIFrameContent
(
refresh
:
boolean
=
false
):
void
{
if
(
!
this
.
_model
||
(
!
refresh
&&
this
.
_lastModelVersion
===
this
.
_model
.
getVersionId
()))
{
// nothing to do
return
;
}
const
html
=
this
.
_model
.
getValue
();
const
iFrameDocument
=
this
.
_iFrameElement
.
contentDocument
;
if
(
!
iFrameDocument
)
{
// not visible anymore
return
;
}
// the very first time we load just our script
// to integrate with the outside world
if
((
<
HTMLElement
>
iFrameDocument
.
firstChild
).
innerHTML
===
'
<head></head><body></body>
'
)
{
iFrameDocument
.
open
(
'
text/html
'
,
'
replace
'
);
iFrameDocument
.
write
(
Integration
.
defaultHtml
());
iFrameDocument
.
close
();
}
// diff a little against the current input and the new state
const
parser
=
new
DOMParser
();
const
newDocument
=
parser
.
parseFromString
(
html
,
'
text/html
'
);
if
(
newDocument
.
head
.
innerHTML
!==
iFrameDocument
.
head
.
innerHTML
)
{
iFrameDocument
.
head
.
innerHTML
=
newDocument
.
head
.
innerHTML
;
}
if
(
newDocument
.
body
.
innerHTML
!==
iFrameDocument
.
body
.
innerHTML
)
{
iFrameDocument
.
body
.
innerHTML
=
newDocument
.
body
.
innerHTML
;
}
this
.
_lastModelVersion
=
this
.
_model
.
getVersionId
();
}
}
namespace
KeybindingEnabler
{
namespace
Integration
{
'
use strict
'
;
...
...
@@ -195,10 +245,4 @@ namespace KeybindingEnabler {
];
return
all
.
join
(
'
\n
'
);
}
export
function
script
()
{
let
result
=
document
.
createElement
(
'
script
'
);
result
.
innerHTML
=
scriptSource
.
join
(
'
\n
'
);
return
result
;
}
}
src/vs/workbench/parts/html/common/htmlInput.ts
浏览文件 @
3b76b471
...
...
@@ -4,9 +4,14 @@
*--------------------------------------------------------------------------------------------*/
'
use strict
'
;
import
URI
from
'
vs/base/common/uri
'
;
import
{
ResourceEditorInput
}
from
'
vs/workbench/common/editor/resourceEditorInput
'
;
export
class
HtmlInput
extends
ResourceEditorInput
{
// just a marker class
getResource
():
URI
{
return
this
.
resource
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录