Skip to content

Instantly share code, notes, and snippets.

View changhuixu's full-sized avatar
💭
Everyone has a happy ending. If you're not happy, it's not the end.

Changhui Xu changhuixu

💭
Everyone has a happy ending. If you're not happy, it's not the end.
View GitHub Profile
function deco(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
console.log(`method "${propertyKey}" decorator: begin`);
if (descriptor === undefined) {
descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
}
const originalMethod = descriptor.value;
@changhuixu
changhuixu / WebBlogsDbContext.cs
Created July 11, 2018 18:26
WebBlogsDbContext.cs
public class WebBlogsDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public virtual DbSet<Author> Authors { get; protected set; }
public WebBlogsDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Author.cs
public class Author
{
public int Id { get; protected set; }
public string FirstName { get; protected set; }
public string LastName { get; protected set; }
private readonly List<Blog> _blogs = new List<Blog>();
public IReadOnlyCollection<Blog> Blogs => _blogs;
}
// AuthorEntityTypeConfiguration.cs
internal class AuthorEntityTypeConfiguration : IEntityTypeConfiguration<Author>
{
public void Configure(EntityTypeBuilder<Author> builder)
{
builder.ToTable("Authors");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).HasColumnName("Id").ValueGeneratedOnAdd();
builder.Property(x => x.FirstName).HasColumnName("FirstName").HasMaxLength(50).IsRequired();
export class CanvasAnimation {
private readonly context: CanvasRenderingContext2D;
constructor(private readonly canvas: HTMLCanvasElement) {
this.context = this.canvas.getContext('2d');
window.requestAnimationFrame(() => this.draw());
}
draw() {
// do stuff
@changhuixu
changhuixu / calculateMouseRelativePositionInCanvas.ts
Created July 13, 2018 21:51
correctly get mouse cursor relative position in canvas
private calculateMouseRelativePositionInCanvas(e: MouseEvent) {
// Note: I have handled scroll effect
this.mousePosition.x =
e.clientX +
(document.documentElement.scrollLeft || document.body.scrollLeft) -
this.canvas.offsetLeft;
this.mousePosition.y =
e.clientY +
(document.documentElement.scrollTop || document.body.scrollTop) -
this.canvas.offsetTop;
@changhuixu
changhuixu / csv-data.service.ts
Last active April 18, 2021 07:01
Export Objects Array as CSV using TypeScript
export class CsvDataService {
static exportToCsv(filename: string, rows: object[]) {
if (!rows || !rows.length) {
return;
}
const separator = ',';
const keys = Object.keys(rows[0]);
const csvContent =
keys.join(separator) +
'\n' +
@changhuixu
changhuixu / quill-editor.service.ts
Created March 24, 2019 02:09
Lazy Load External JavaScript Library in Angular
import { Injectable, Inject } from '@angular/core';
import { ReplaySubject, Observable, forkJoin } from 'rxjs';
import { DOCUMENT } from '@angular/common';
@Injectable({
providedIn: 'root'
})
export class QuillEditorService {
private _loadedLibraries: { [url: string]: ReplaySubject<any> } = {};
@changhuixu
changhuixu / quill-editor.component.ts
Created March 24, 2019 03:31
Lazy Load External JavaScript Library in Angular
declare var Quill: any;
@Component({
//...
})
export class QuillEditorComponent implements OnInit {
constructor(
// ...
private readonly svc: QuillEditorService,
public class LotNamesCache : ILotNamesCache
{
public const string CacheKey = nameof(LotNamesCache);
private readonly ImmutableCacheDbContext _dbContext;
private readonly IMemoryCache _cache;
public LotNamesCache(ImmutableCacheDbContext dbContext, IMemoryCache cache)
{
_dbContext = dbContext;
_cache = cache;