質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

Q&A

1回答

1188閲覧

vue.jsとtypescriptを用いてハイライトを行う単語を検索して指定をしたい

syen2501

総合スコア38

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

0グッド

0クリップ

投稿2019/06/29 07:57

私は、typescriptで特定の語を文ごとにハイライトするというプログラムを作成しています。
さらに私は、追加機能として、単語を検索して、ヒットしたものをハイライトするかしないかを
チェックボックスで選択しハイライトで表示もしくはハイライトを消す機能を作成しようとしています。

この追加機能は、vue.jsで作成しようと考えているのですが、どのようにvue.jsに
typescriptのプログラムを追加すれば良いか良く分かりません。
初心者なので、申し訳ありませんが教えて頂ければ幸いです。宜しくお願い致します。

typescript

1'use strict'; 2import * as vscode from 'vscode'; 3import * as fs from 'fs'; 4 5export function activate(context: vscode.ExtensionContext) { 6 console.log('Congratulations, your extension "semieditor" is now active!'); 7 const readsemiJson = JSON.parse(fs.readFileSync('C:\User\specification\src\sample.json','utf8')); 8 9 // 特定の単語に色付け 10 const strengthTimeWordList = readsemiJson.強時間; 11 12 const strengthTimeWords: vscode.DecorationOptions[] = []; 13 const dependencyRelationWords: vscode.DecorationOptions[] = []; 14 15 //child -> parent(値域) semiについて 16 function fromObjecttoRange(WordList: any,foundWordList: vscode.DecorationOptions[],dependencyRelationList: vscode.DecorationOptions[],Count: number){ 17 const activeEditor = vscode.window.activeTextEditor; 18 const text = activeEditor.document.getText(); //ドキュメント取得 19 20 // 取得したテキストを改行ごと(文章)に分割する 21 var textArray = text.split(/\r\n|\r|\n/); 22 23 if(WordList.length > 0){ 24 for(var i = 0; i < WordList.length; i++){ 25 const word = WordList[i].split('=>'); 26 const parentword = word[0]; 27 const rangeword = word[1]; 28 var offSet = 0; 29 for(var j = 0; j < textArray.length; j++){ 30 if(textArray[j] !== ''){ 31 if(textArray[j].search(rangeword) !== -1 && textArray[j].search(parentword) !== -1){ 32 const startPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword)); 33 const endPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword) + rangeword.length); 34 const decoration = { range: new vscode.Range(startPos, endPos) }; 35 foundWordList.push(decoration); 36 37 const related_startPos = activeEditor.document.positionAt(offSet + textArray[j].search(parentword)); 38 const related_endPos = activeEditor.document.positionAt(offSet + textArray[j].search(parentword) + parentword.length); 39 const related_decoration = { range: new vscode.Range(related_startPos, related_endPos) }; 40 dependencyRelationList.push(related_decoration); 41 } 42 if(textArray[j].search(rangeword) !== -1 && parentword === 'なし'){ 43 const startPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword)); 44 const endPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword) + rangeword.length); 45 const decoration = { range: new vscode.Range(startPos, endPos) }; 46 foundWordList.push(decoration); 47 } 48 } 49 //改行の長さも加算している 50 offSet += textArray[j].length + 2; 51 Count++; 52 } 53 } 54 return {foundWordList,dependencyRelationList,Count}; 55 } 56 } 57 58 //値域と修飾語をハイライト 59 function decorateWord() { 60 const activeEditor = vscode.window.activeTextEditor; 61 62 //強時間 63 fromObjecttoRange(strengthTimeWordList,strengthTimeWords,dependencyRelationWords,strengthtimeCount); 64 65 activeEditor.setDecorations(strengthTimeMarkerDecoration,strengthTimeWords); 66 activeEditor.setDecorations(dependencyRelation,dependencyRelationWords); 67 } 68 69 //ハイライトの削除 70 function clearHighlight(){ 71 //強時間 72 fromObjecttoRange(strengthTimeWordList,strengthTimeWords,dependencyRelationWords,strengthtimeCount); 73 74 strengthTimeMarkerDecoration.dispose(); 75 dependencyRelation.dispose(); 76 77 } 78 context.subscriptions.push(vscode.commands.registerCommand('extension.color', () => { 79 vscode.window.showInformationMessage("Color Range Word"); 80 decorateWord(); 81 })); 82 83 context.subscriptions.push(vscode.commands.registerCommand('extension.highlightOFF', () => { 84 vscode.window.showInformationMessage("HighLight OFF"); 85 clearHighlight(); 86 })); 87 88 //強時間に色付け 89 const strengthTimeMarkerDecoration = vscode.window.createTextEditorDecorationType({ 90 'borderWidth': '1px', 91 'borderRadius': '2px', 92 'borderStyle': 'solid', 93 'backgroundColor': 'rgba(128, 128, 0, 0.3)', 94 'color':'orange' 95 }); 96 97 //係り受けの対象を色付け 98 const dependencyRelation = vscode.window.createTextEditorDecorationType({ 99 'borderWidth': '1px', 100 'borderRadius': '2px', 101 'borderStyle': 'solid', 102 'backgroundColor': 'rgba(0, 255, 0, 0.3)', 103 'color':'lightgreen' 104 }); 105}

JSON

1{ 2 "回数": [], 3 "強数量修飾": [], 4 "時数ノ": [], 5 "未来句": [], 6 "数量": [], 7 "強時間": [ 8 "ドア入力右=>常時", 9 "なし=>常時", 10 "ドア入力I=>常時", 11 "なし=>長時間", 12 "扉閉め忘れ=>一定時間以上" 13 ], 14 "時間": [] 15}

Vue

1// Home.vue 2 3<template> 4 <div class="home"> 5 <Button></Button> 6 <input v-model="message" placeholder="" style = "position: absolute; left: 10px; top: 10px"> 7 </div> 8</template> 9 10<script lang="ts"> 11 import { Component, Vue } from 'vue-property-decorator'; 12 import Button from "@/views/button.vue"; 13 14 @Component({ 15 components: { 16 Button, 17 }, 18 }) 19 export default class Home extends Vue {} 20</script> 21

Vue

1//button.vue 2<template> 3 <button style="position: absolute; left: 190px; top: 9px">検索</button> 4</template> 5 6<script lang="ts"> 7 import {Component, Vue} from "vue-property-decorator"; 8 9 @Component 10 export default class MyButton extends Vue{ 11 } 12</script>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KuwabataK

2019/06/29 18:23

ざっと読んだ感じ、TypeScriptのコードはvscodeのExtensionとして開発されているようですがあっているでしょうか? まずはっきりさせておきたいのが、vscodeのExtensionとして開発されたものをVueでも流用したいということなのか(つまりブラウザ上で動かしたい) vscodeのExtensionをVueで開発したいのか(つまりvscodeのExtensionを開発したい)、どちらなんでしょう?
syen2501

2019/06/29 18:51

行いたいことは、vscodeのExtensionとして開発されたものに機能を追加したい。 追加するための方法としてvueを使ってフォームを作成しより使いやすい機能として実装するという感じです。 なので、ブラウザ上で動かす必要はありません。
syen2501

2019/06/30 05:29

返信ありがとうございます。 例えば、typescript内で検索を行うコマンドを登録しておいて、vscode内でそのコマンドを実行した際に、 あらかじめhtml等で作成しておいたフォームを呼び出すようなことは可能なのでしょうか? 可能であれば、教えて頂ければ幸いです。宜しくお願い致します。
guest

回答1

0

Vue.jsはブラウザ上で動作されることを前提に作られたフレームワークなので、私の知る限り、vscodeのExtension上では直接扱うことはできません。

一応公式サイトを見る限りでは、vscode上で、ビュワーとしてWebViewを動かすことはできるので、そこでならvueで作成したビルド後のjsファイルを動作させてフォームなどを表示させることはできるかもしれませんが、vscodeのAPIにはVue上から直接アクセスできないので、仮に検索フォームを作ったとしても、vscode上ではなんの動作もしない役に立たないフォームができるだけじゃないかなと思います。

一応、WebView側からvscode側にイベントを投げることはできるみたいなので、ここらへんのAPIを駆使すれば動作する検索フォームを作ることはできるかもしれませんが、かなり茨の道になることが予想されます。(というかフォームぐらいの簡単なものでよいのであれば、vueを使うよりも素のhtmlとJSを使ったほうが、vscodeには組み込みやすいと思います)

なのでvscodeのExtensionを作るということであれば、一旦Vueで作ることは諦めて、vscodeに用意された標準APIの範囲内でできるかどうかを検討してみてはどうでしょうか?

投稿2019/06/29 19:28

編集2019/06/29 19:34
KuwabataK

総合スコア306

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

syen2501

2019/06/30 05:39

返信ありがとうございます。 例えば、typescript内で検索を行うコマンドを登録しておいて、vscode内でそのコマンドを実行した際に、 あらかじめhtml等で作成しておいたフォームを呼び出すようなことは可能なのでしょうか? 可能であれば、教えて頂ければ幸いです。宜しくお願い致します。
KuwabataK

2019/06/30 05:42

それはできると思います。ただ、WebViewは別タブで表示されてしまうので、その中でフォームを表示するような形になるかと思います。(テキストエディタの上にオーバーレイさせて表示させるようなことはできない) 詳しくは以下のリンクを見てください。 https://code.visualstudio.com/api/extension-guides/webview 多分以下のようなコードを書くことになると思います。(上記のリンクに完全なサンプルがあるので詳しくはそちらを見てください) // web viewを呼び出し const panel = vscode.window.createWebviewPanel( 'catCoding', 'Cat Coding', vscode.ViewColumn.One, {} ); // webviewに表示したいhtmlを登録 panel.webview.html = `<!DOCTYPE html> .... (表示したいhtmlの内容) `;
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問