8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Laravel] TOAST UI Editorの実装

Posted at

Markdownエディターの実装をしていきます。 今回使用するのは、こちらの TOAST UI Editorを使用します。

CDNでサクッと実装していきますね。(npmでトライしたのですが、エラーが止まらなくて諦めました・・・。もしやnpmで実装できた方がいらっしゃれば、オリエンテーションしてください・・・。)

bladeの編集

viewファイルに、(1)CSSファイルの読み込み、(2)エディタのコンポーネント、(3)JSファイル読み込みを書き込んでいきます。

create.blade.php(個別のblade)
@extends('layouts.app')

// CSSファイルの読み込み
@section('css')
<link rel="stylesheet" href="https://uicdn.toast.com/tui-editor/latest/tui-editor.css">
<link rel="stylesheet" href="https://uicdn.toast.com/tui-editor/latest/tui-editor-contents.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/codemirror.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
@endsection

// マークダウンエディタを表示させたい場所に記述しよう。
@section('content')
     <div id="editSection"></div>
@endsection

// JSファイル読み込み
@section('javascript-footer')
<script src="https://uicdn.toast.com/tui-editor/latest/tui-editor-Editor-full.js"></script>
<script src="{{ asset('js/jquery.imgpreview.js') }}"></script>
@endsection

ちなみに、個別のviewファイルを参照している親テンプレートは、以下のような感じになっています。

app.blade.php(親テンプレート)
// headタグ内で個別のCSS呼び出し
<head>
    @yield('css')
</head>

// bodyタグ終了前に個別のJSファイルの呼び出し
<body>
    @yield('content')

    @yield('javascript-footer')
</body>

JSファイルの作成

create.blade.phpの<script src="{{ asset('js/jquery.imgpreview.js') }}"></script>部分で読み込んでいる、JSファイルを作成します。
public/jsの下に、.jsファイルを作成(今回はjquery.imgpreview.jsと命名)します。Documentを丸写しします。

var editor = new tui.Editor({
    el: document.querySelector('#editSection'),
    previewStyle: 'vertical',
    height: '300px',
    initialEditType: 'markdown'
});

ファイルを保存して、終了!
以下のような感じで実装できました!!

スクリーンショット 2020-01-12 20.37.32.png
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?