Keeping Your Business And Clients Safe With Digital Policies
Now that we have our view and component layer, we need something to manage state and the lifecycle of our application. Redux is also a clear winner here.
Alongside React, Facebook presented a design pattern for one way data flow called Flux. Flux largely delivered on its promise of simplifying state management, but it also brought with it more questions, such as how to store that state and where to do Ajax requests. To answer those questions, countless frameworks were built on top of the Flux pattern: Fluxible, Reflux, Alt, Flummox, Lux, Nuclear, Fluxxor and many, many more. One Flux-like implementation eventually caught the community’s attention, and for good reasons: Redux.
One thing to be careful is the natural instinct to try and abstract away the Redux boilerplate. There’s good reasons behind all those pieces. Make sure you tried it and understand the “why” before trying to blindly improve on it.
Why Javascript?
JavaScript is the standard language of the web platform. Learning it will allow you to build any app you want for any device or platform you want. Watch this 1 minute video to learn more about what JavaScript can do for you:
The essentials in order:
- Newbie friendly: Make a Star Wars Game with Hour of Code (free website)
- Newbie friendly: “JavaScript for Cats”, Max Ogden (free website)
- Newbie friendly: FreeCodeCamp’s 10-hour Basic JavaScript track You can come back to FCC for more practice while you explore the rest of these resources. I recommend that you dedicate time to study other resources and do some fresh FCC exercises every day. There are hundreds of hours of exercises, in total, covering a lot more than just JavaScript.
- Newbie friendly: “JavaScript for Cats”, Max Ogden (free website)
- “Eloquent JavaScript: Second Edition”, Marijn Haverbeke (book with free online edition)
Why Universal?
Nobody else has this understanding of the word isomorphic. Words are only useful if they communicate information, but they can only communicate information if we can agree on their definition.
The troubling thing about the word “isomorphic” is that it already has a well understood definition in the context of functional programming, one of the pillar paradigms of JavaScript.
Lots of people think of the mathematical / FP definition borrowed from category theory. An isomorphism is a pair of functions (morphisms) such that:
var Prism = require('prismjs');
// The code snippet you want to highlight, as a string
var code = "var data = 1;";
// Returns a highlighted HTML string
var html = Prism.highlight(code, Prism.languages.javascript);
I implemented Prism.js based code syntax highlight feature for my blog. This blog post shows how to use Prism with Angular.
Prism is a lightweight, extensible syntax highlighter, built with modern web standards in mind.
There are few existing Angular solutions for Prism:
- https://github.com/ngx-prism/core
- https://github.com/vaibhav93/angular-prism
- https://github.com/tpadjen/ng2-prism
But I decided not to use the existing ones because all the solutions use a component which requires code blocks as a content or as a parameter. Content of my blog is fetched from REST API as HTML so I can't (easily) separate code blocks from other content. That's why I decided to see how to implement support for the Prism without existing components. I use Prism's highlightAll method to process all of the pre and code tags once.
Implementing following steps needs a basic knowledge about Angular development.
1. Install Prism from npm
$ npm install prismjs --save
2. Create highlight service
Prism.js dependencies are imported in this service. Here you can add supported languages and additional Prism plugins you might want to use.
import { Injectable, Inject } from '@angular/core';
import { PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import 'clipboard';
import 'prismjs';
import 'prismjs/plugins/toolbar/prism-toolbar';
import 'prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard';
import 'prismjs/components/prism-css';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-java';
import 'prismjs/components/prism-markup';
import 'prismjs/components/prism-typescript';
import 'prismjs/components/prism-sass';
import 'prismjs/components/prism-scss';
declare var Prism: any;
@Injectable()
export class HighlightService {
constructor(@Inject(PLATFORM_ID) private platformId: Object) { }
highlightAll() {
if (isPlatformBrowser(this.platformId)) {
Prism.highlightAll();
}
}
}
Add service into app.module.ts
providers: [
...
HighlightService,
],
My implementation doesn't support server side rendering at the moment. Code syntax highlighting is enabled only for a browser because Prism.highlightAll
uses document
which is not available in Node.js. There probably are workarounds for this but I don't see code highlighting that important for server side rendering.
Prism supports 130 languages so I only import the ones I really need.
3. Use highlight service in your component
Then let's add the service into a component which handles data we want to highlight.
Lifecycle hook AfterViewChecked is used here to call the highlight service when view is ready. Method ngAfterViewChecked
might be called multiple times. I use highlighted
boolean to check if highlighting is already done to prevent multiple highlightAll
method calls.
import { Component, OnInit, AfterViewChecked } from '@angular/core';
import { HighlightService } from '../shared/utils/highlight.service';
@Component({
selector: 'app-blog-post',
templateUrl: './blog-post.component.html',
styleUrls: ['./blog-post.component.scss']
})
export class BlogPostComponent implements OnInit, AfterViewChecked {
blogPost: BlogPostInterface;
highlighted: boolean = false;
constructor(
...
private highlightService: HighlightService) {
}
/**
* Highlight blog post when it's ready
*/
ngAfterViewChecked() {
if (this.blogPost && !this.highlighted) {
this.highlightService.highlightAll();
this.highlighted = true;
}
}
/**
* Fetch blog post from API
*/
ngOnInit() {
...
}
}
This code is just a partial example how to use recently created service in your own component. You need to adapt usage for your custom component.
4. Add Prism styles
Next add Prism styles into your styles.scss
. Theme CSS is needed at least. Check my earlier blog post how to change Angular to use SCSS instead of CSS if you don't use SCSS already. Alternatively you can add CSS files directly into your HTML.
@import "~prismjs/plugins/toolbar/prism-toolbar.css";
@import "~prismjs/themes/prism-okaidia";
It should be working now. Please feel free to comment this post if you have questions or suggestions how to improve code base.
In JavaScript, variables outside the scope of a top-level function are global (meaning, everyone can access them). Because of this, it’s common to have “namespace pollution”, where completely unrelated code shares global variables.
var code = "var data = 1;";
var strippedCode = code.replace(/^\s+/gm, ''); // Remove leading whitespace
var highlightedCode = Prism.highlight(strippedCode, Prism.languages.javascript);
document.write(highlightedCode);
Good luck!