英文:
Adding tab character inside code tag (Angular-typescript)
问题
我正在使用Angular和TypeScript进行这个项目。
我尝试在HTML的<code>
标签内部添加制表符,以便在页面中格式化SQL代码,但我没有成功。
HTML代码:
<div class="documentationCode" id="documentationContainer-{{nodeToPass.xmlNode.nameId}}">
<pre class="preCode">
<code id="editor"
tabindex="-1"
contenteditable="true"
(focusout)="emitDataSave(nodeToPass, $event)"
[highlight]="codice"
(keydown)="check($event)"
class="code">
</code>
</pre>
</div>
TypeScript代码:
check(event: any) {
if (event.keyCode === 9) /*捕获制表符*/
{
event.preventDefault();
/*用\t替换制表符*/
}
else {
return
}
}
我尝试使用在<textarea>
中有效的相同方法,但在<code>
中不起作用,主要是因为我无法获取插入制表符的光标位置。
我不能用<textarea>
标签替换<code>
,因为我需要它来格式化SQL,而且它还具有我项目所需的内置高度调整功能。
我无法使用event.target.selectionStart
/ event.target.selectionStart
,因为它们都返回undefined
。
我必须将制表符放在光标所在的位置。
有没有关于在<code>
标签内部使用制表符进行HTML文本格式化的解决方法?
英文:
I am using Angular and typescript for this project
I was trying to add the tab character inside of the <code
> tag in HTML, for formatting sql code in a page, but I had no luck with that.
HTML code
<div class="documentationCode" id="documentationContainer-{{nodeToPass.xmlNode.nameId}}">
<pre class="preCode">
<code id="editor"
tabindex="-1"
contenteditable="true"
(focusout)="emitDataSave(nodeToPass,$event)"
[highlight]="codice"
(keydown)="check($event)"
class="code">
</code>
</pre>
</div>
typescript code
check(event:any)
{
if(event.keyCode === 9) /*catch tab character*/
{
event.preventDefault();
/*REPLACE TAB WITH \t*/
}
else
{
return
}
}
I tried using the same method that works with the <textarea
> , but it doesn't work with the <code
>, as far as I have seen, mainly because I cannot get the caret position for knowing where to add the tab character
I cannot replace the <code
> tag with the <textarea
>, since I need it for sql formatting, also it has built-in height resizing, which I need for my project.
I cannot use event.target.selectionStart
/ event.target.selectionStart
since they both return undefined
I have to place the tab character where the caret is
Is there any workaround for formatting html text inside of the <code
> tag, with the tab character?
答案1
得分: 0
找到了解决方案:
我使用了一个 Angular 版本的 'codejar',你可以在这个链接找到:https://github.com/julianpoemp/ngx-codejar
我导入了这些模块,现在可以无问题地添加标签页了。如果你想要图形化的东西(语法高亮),你需要将 CSS 导入到你自己的程序中,因为出于某些原因,它无法正常工作(我使用了 highlightjs,CSS 可在 nodemodule 文件夹下的 highlightjs 文件夹中找到 - ngx-codejar 支持 hljs 和 prismjs 两者)。
另一件需要记住的事情是,该模块会用自己的样式覆盖你的样式,所以,就像在我的情况下,如果你需要一些特定的样式,你必须在你的 CSS 中添加 '!important' 属性。
以下是代码:
app.module.ts:
declarations: [ ... ],
imports: [
...
NgxCodejarModule,
...
],
providers: [
{
provide: HIGHLIGHT_OPTIONS,
useValue: <HighlightOptions>{
coreLibraryLoader: () => import('highlight.js/lib/core'),
themePath: '/node_modules/highlight.js/styles/github.css',
languages: {
sql: () => import('highlight.js/lib/languages/sql'),
},
},
},
],
HTML:
<ngx-codejar
id="editor"
[highlightMethod]="highlightMethod"
[(code)]="code"
(focusout)="emitDataSave(nodeToPass,$event)"></ngx-codejar>
Typescript:
highlightMethod(editor: CodeJarContainer) {
if (editor.textContent !== null && editor.textContent !== undefined)
{
editor.innerHTML = hljs.highlight(editor.textContent, { language: 'sql' }).value;
}
}
CSS:
.hljs {
font-size: 12px !important;
font-family: 'Courier New', Courier, monospace !important;
min-height: fit-content !important;
color: #24292e;
background: #fff
} /*加上 highlightjs 中的所有 CSS*/
英文:
Found the solution:
I used an angular version of 'codejar' which is found at this link https://github.com/julianpoemp/ngx-codejar
I imported the modules and now I am able to add tab without any issues
If you want the graphical thing too (the syntax highlight) you need to import the css into your own program, since, for some reasons, it doesn't work properly (I used highlightjs, the css is found inside the nodemodule folder, under the highlightjs folder - ngx-codejar supports both hljs and prismjs)
Another thing to remember, the module overwrite your style with its own, so, like in my case, need some particular style, you have to add the '!important' attribute in your css
Here is the code:
app.module.ts:
declarations: [ ... ],
imports: [
...
NgxCodejarModule,
...
],
providers: [
{
provide: HIGHLIGHT_OPTIONS,
useValue: <HighlightOptions>{
coreLibraryLoader: () => import('highlight.js/lib/core'),
themePath: '/node_modules/highlight.js/styles/github.css',
languages: {
sql: () => import('highlight.js/lib/languages/sql'),
},
},
},
],
HTML:
<ngx-codejar
id="editor"
[highlightMethod]="highlightMethod"
[(code)]="code"
(focusout)="emitDataSave(nodeToPass,$event)">
</ngx-codejar>
Typescript:
highlightMethod(editor: CodeJarContainer) {
if (editor.textContent !== null && editor.textContent !== undefined)
{
editor.innerHTML = hljs.highlight(editor.textContent, { language: 'sql' }).value;
}
}
CSS:
.hljs {
font-size: 12px !important;
font-family: 'Courier New', Courier, monospace !important;
min-height: fit-content !important;
color: #24292e;
background: #fff
} /*plus the whole css found in highlightjs*/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论