/*

  This file includes the CodeMirror and EasyMDE Styles.
  And we customized it heavily to fit the Blogifier

  github.com/blogifierdotnet/Blogifier
  github.com/farzindev

  1. Container
  2. CodeMirror
  3. EasyMDE
  4. EasyMDE Toolbar
  5. EasyMDE Preview
  6. EasyMDE Theme
  7. EasyMDE Statusbar

*/

// Container
.EasyMDEContainer {
  display: block;
  position: relative;
  padding-top: 3rem;
  overflow: hidden;
  @include bf-shadow;
}

//
// CodeMirror
//
.CodeMirror {
  position: relative;
  overflow: hidden;
  // box-sizing: border-box; // TODO: Check this
  height: auto;
  border: none;
  padding: 0;
  font: inherit;
  z-index: 0;
  word-wrap: break-word;
}

// hide the CodeMirror-scroll when we are in preview.
.disabled-for-preview + .CodeMirror > .CodeMirror-scroll {
  display: none !important;
}

.CodeMirror-scroll {
  overflow: scroll !important;
  height: 100%;
  outline: 0;
  position: relative;
  cursor: text;
  // hide the scrollbar
  margin-right: -15px;
  margin-bottom: -15px;
  padding-bottom: 50px;
}
.CodeMirror-sizer {
  position: relative;
  border-right: 0 !important;
}
.CodeMirror-hscrollbar,
.CodeMirror-scrollbar-filler,
.CodeMirror-vscrollbar {
  position: absolute;
  z-index: 6;
  display: none;
  outline: 0;
}
.CodeMirror-vscrollbar {
  right: 0;
  top: 0;
  overflow-x: hidden;
  overflow-y: scroll;
}
.CodeMirror-hscrollbar {
  bottom: 0;
  left: 0;
  overflow-y: hidden;
  overflow-x: scroll;
}
.CodeMirror-scrollbar-filler {
  right: 0;
  bottom: 0;
}
.CodeMirror-lines {
  cursor: text;
  min-height: 1px;
  padding: 1rem;
}
.CodeMirror pre.CodeMirror-line,
.CodeMirror pre.CodeMirror-line-like {
  border-radius: 0;
  border-width: 0;
  background: 0 0;
  font-family: inherit;
  font-size: 1rem;
  margin: 0;
  white-space: pre;
  word-wrap: normal;
  line-height: 1.618;
  color: inherit;
  z-index: 2;
  position: relative;
  overflow: visible;
  font-variant-ligatures: contextual;
}
.CodeMirror-wrap pre.CodeMirror-line,
.CodeMirror-wrap pre.CodeMirror-line-like {
  word-wrap: break-word;
  white-space: pre-wrap;
  word-break: normal;
}
.CodeMirror-rtl pre {
  direction: rtl;
}
.CodeMirror-code {
  outline: 0;
}
.CodeMirror-scroll,
.CodeMirror-sizer {
  box-sizing: content-box;
}
.CodeMirror-measure {
  position: absolute;
  width: 100%;
  height: 0;
  overflow: hidden;
  visibility: hidden;
}
.CodeMirror-cursor {
  position: absolute;
  pointer-events: none;
}
.CodeMirror-measure pre {
  position: static;
}
div.CodeMirror-cursors {
  visibility: hidden;
  position: relative;
  z-index: 3;
}
div.CodeMirror-dragcursors {
  visibility: visible;
}
.CodeMirror-focused div.CodeMirror-cursors {
  visibility: visible;
}
.cm-tab-wrap-hack:after {
  content: "";
}
.cm-spell-error:not(.cm-url):not(.cm-comment):not(.cm-tag):not(.cm-word):not(.cm-string):not(.cm-attribute):not(.cm-image-alt-text) {
  border-bottom: 1px solid #ff8e8e;
  padding-bottom: 0.125rem;
}
.CodeMirror-cursor {
  width: 1px;
  background-color: #000;
}

//
// EasyMDE
//
.CodeMirror-fullscreen {
  background: #fff;
  position: fixed !important;
  top: 3rem;
  left: 0;
  right: 0;
  bottom: 0;
  height: auto;
  z-index: 15;
  border-right: none !important;
  border-bottom-right-radius: 0 !important;

  @media screen and (min-width: 1024px) {
    .CodeMirror-lines {
      padding: 2.5rem 2.5rem 20rem !important;
    }
  }
  @media screen and (max-width: 1023px) {
    .CodeMirror-lines {
      padding: 1.5rem 1.5rem 20rem !important;
    }
  }
}

.CodeMirror-sided {
  width: 50% !important;
}
.CodeMirror-placeholder {
  opacity: 0.5;
}
.CodeMirror-selected {
  background-color: $gray-200;
}
.CodeMirror-selectedtext {
  color: #000;
}

//
// EasyMDE Toolbar
//
.editor-toolbar {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  user-select: none;
  border-bottom: 1px solid $gray-200;
  display: flex;
  align-items: center;
  z-index: 5;
  height: 2.5rem;

  &.fullscreen {
    width: 100%;
    height: 2.5rem;
    box-sizing: border-box;
    background: #fff;
    position: fixed;
    top: 0;
    left: 0;
    opacity: 1;
    z-index: 100;
    button {
      height: 2.5rem;
    }
    &::after {
      content: "";
      display: block;
      width: 100%;
      position: absolute;
      bottom: -1rem;
      left: 0;
      height: 1rem;
      background-image: linear-gradient(
        180deg,
        rgba(0, 0, 0, 0.1),
        rgba(0, 0, 0, 0) 30%
      );
      z-index: 6;
    }
  }
  i.separator {
    font-size: 0;
    width: 1px;
    height: 100%;
    background: #e8ecef;
  }
  button {
    flex-grow: 1;
    display: block;
    text-align: center;
    background: none;
    border: 0;
    width: 2.5rem;
    max-width: 2.5rem;
    height: 2.5rem;
    margin: 0;
    padding: 0;
    cursor: pointer;

    &:hover {
      background-color: #f3f5f9;
      svg {
        fill: #000;
      }
    }

    &.active {
      background-color: rgba($blogifier, 0.1);
      svg {
        fill: $blogifier;
      }
    }

    svg {
      fill: $gray-700;
      transform: translateY(-1px);
    }
  }
  &.disabled-for-preview {
    button:not(.no-disable) {
      opacity: 0.5;
      pointer-events: none;
      background-color: #fff !important;
      border-bottom-color: transparent !important;
      svg {
        fill: $gray-800 !important;
      }
    }
  }
}

.toolbar-sticky {
  .editor-toolbar {
    width: 61rem;
    position: fixed;
    top: 0;
    left: auto;
    background-color: #fff;
    border-bottom-color: $gray-300;

    @media screen and (max-width: 1199px) {
      width: 100%;
      left: 0;
    }
  }
}

//
// EasyMDE Preview
//
.editor-preview {
  font-size: 1rem;
  padding: 1rem;
  background: #fff;
  line-height: 1.618;

  &-full {
    width: 100%;
    height: 100%;
    z-index: 15;
    overflow: auto;
    display: none;
    box-sizing: border-box;
  }
  &-side {
    position: fixed;
    bottom: 0;
    width: 50%;
    top: 3rem;
    right: 0;
    z-index: 15;
    overflow: auto;
    display: none;
    box-sizing: border-box;
    word-wrap: break-word;
    border-left: 1px solid $gray-200;
    @media screen and (min-width: 1024px) {
      padding: 2.5rem 2.5rem 20rem !important;
    }
    @media screen and (max-width: 1023px) {
      padding: 1.5rem 1.5rem 20rem !important;
    }

    p {
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  &-active-side,
  &-active {
    display: block;
  }

  a {
    color: $primary;
    text-decoration: underline;
    &:hover {
      color: rgba($primary, 0.8);
    }
  }
  blockquote,
  pre,
  ul,
  ol,
  p {
    margin-bottom: 2rem;
  }
  blockquote {
    font-weight: 300;
    position: relative;
    padding: 0 3rem;
    &::before,
    &::after {
      position: absolute;
      content: "";
      font-size: 5rem;
      width: 2.5rem;
      height: 2.5rem;
      background-image: url("data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjxzdmcgaGVpZ2h0PSI0OCIgdmlld0JveD0iMCAwIDQ4IDQ4IiB3aWR0aD0iNDgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTEyIDM0aDZsNC04di0xMmgtMTJ2MTJoNnptMTYgMGg2bDQtOHYtMTJoLTEydjEyaDZ6Ii8+PHBhdGggZD0iTTAgMGg0OHY0OGgtNDh6IiBmaWxsPSJub25lIi8+PC9zdmc+");
      display: block;
      background-size: 2.5rem;
      background-repeat: no-repeat;
      opacity: 0.3;
    }

    &::before {
      top: 0;
      left: 0;
      transform: rotate(180deg);
    }

    &::after {
      bottom: 0;
      right: 0;
    }
    p {
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  pre {
    background: $gray-100;
  }
  table td,
  table th {
    border: 1px solid $gray-200;
    padding: 5px;
  }
  img,
  video,
  iframe {
    max-width: 100%;
  }
  table,
  iframe,
  audio,
  video {
    margin-bottom: 2rem;
    width: 100%;
  }
}

//
// EasyMDE Theme
//
.cm-s-easymde {
  .cm-tab {
    display: inline-block;
    text-decoration: inherit;
  }
  .cm-header,
  .cm-strong {
    font-weight: 500;
  }
  .cm-em {
    font-style: italic;
  }
  .cm-strikethrough {
    text-decoration: line-through;
  }
  .cm-tag {
    color: $hljs-green;
  }
  .cm-attribute {
    color: $hljs-blue;
  }
  .cm-string {
    color: $hljs-red;
  }
  .cm-header-1 {
    font-size: 175%;
    line-height: 100%;
  }
  .cm-header-2 {
    font-size: 160%;
    line-height: 160%;
  }
  .cm-header-3 {
    font-size: 125%;
    line-height: 125%;
  }
  .cm-header-4 {
    font-size: 110%;
    line-height: 110%;
  }
  .cm-comment {
    background: $gray-200;
    border-radius: 2px;
    padding-top: 0.25rem;
    padding-bottom: 0.25rem;
    border-radius: 0.125rem;
    color: $gray-900;
    &.cm-formatting:not(.cm-comment + .cm-formatting) {
      font-weight: 500;
      padding-left: 0.25rem;
    }

    + .cm-formatting {
      font-weight: 500;
      padding-right: 0.25rem;
    }
  }
  .cm-link {
    color: $hljs-blue;
    text-decoration: none;
  }
  .cm-image {
    color: $hljs-green;
    ~ .cm-url {
      color: $hljs-green;
    }
  }
  .cm-url {
    color: $hljs-blue;

    &.cm-string {
      font-weight: 300;
    }
  }
  .cm-quote {
    color: $gray-800;
    font-weight: 300;
    font-style: italic;
    &::after {
      content: "";
      display: block;
      width: 2px;
      position: absolute;
      left: -0.75rem;
      top: 0;
      bottom: 0;
      background-color: $gray-500;
    }
  }
  .cm-hr {
    display: block;
    color: $gray-500;
    font-size: 0.785rem;
    width: 100%;
    position: absolute;
    text-rendering: geometricPrecision;
    &::after {
      content: "";
      display: block;
      position: absolute;
      top: 50%;
      margin-top: 0;
      left: 1.25rem;
      right: 0;
      height: 1px;
      background-color: $gray-500;
      z-index: 1;
    }
  }
  .cm-invalidchar {
    color: $hljs-red;
  }
  .cm-negative {
    color: $hljs-red;
  }
  .cm-positive {
    color: $hljs-green;
  }
}

// preview image
span[data-img-src]::after {
  display: block;
  content: "";
  height: 0;
  background-image: var(--bg-image);
  background-repeat: no-repeat;
  background-size: contain;
  max-height: 100%;
  max-width: 100%;
  padding-top: var(--height);
  width: var(--width);
}

.cm-formatting {
  &-list {
    padding-left: 1.5rem;
  }
  &-link {
    &.cm-link {
      font-weight: 600;
      &:not(.cm-link + .cm-formatting.cm-link):not(.cm-formatting-image) {
        &::before {
          transform: translateY(3px);
          content: "";
          display: inline-block;
          width: 1rem;
          height: 1rem;
          margin-right: 0.0625rem;
          background-repeat: no-repeat;
          background-size: 1rem;
          background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' fill='%23006ee0' class='bi bi-link-45deg' viewBox='0 0 16 16'%3E%3Cpath d='M4.715 6.542 3.343 7.914a3 3 0 1 0 4.243 4.243l1.828-1.829A3 3 0 0 0 8.586 5.5L8 6.086a1.002 1.002 0 0 0-.154.199 2 2 0 0 1 .861 3.337L6.88 11.45a2 2 0 1 1-2.83-2.83l.793-.792a4.018 4.018 0 0 1-.128-1.287z'%3E%3C/path%3E%3Cpath d='M6.586 4.672A3 3 0 0 0 7.414 9.5l.775-.776a2 2 0 0 1-.896-3.346L9.12 3.55a2 2 0 1 1 2.83 2.83l-.793.792c.112.42.155.855.128 1.287l1.372-1.372a3 3 0 1 0-4.243-4.243L6.586 4.672z'%3E%3C/path%3E%3C/svg%3E");
        }
      }
    }
    &-string {
      &.cm-url {
        font-weight: 600 !important;
      }
    }
  }
  &-image {
    &.cm-image {
      font-weight: 600;
      &.cm-image-marker {
        font-weight: 400;
        &::before {
          transform: translateY(1px);
          content: "";
          display: inline-block;
          width: 0.875rem;
          height: 0.875rem;
          margin-right: 0.1785rem;
          background-repeat: no-repeat;
          background-size: 0.875rem;
          background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23168174' class='bi bi-image' viewBox='0 0 16 16'%3E%3Cpath d='M6.002 5.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0z'%3E%3C/path%3E%3Cpath d='M2.002 1a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V3a2 2 0 0 0-2-2h-12zm12 1a1 1 0 0 1 1 1v6.5l-3.777-1.947a.5.5 0 0 0-.577.093l-3.71 3.71-2.66-1.772a.5.5 0 0 0-.63.062L1.002 12V3a1 1 0 0 1 1-1h12z'%3E%3C/path%3E%3C/svg%3E");
        }
      }
    }
  }
}

//
// EasyMDE Statusbar
//
.editor-statusbar {
  padding: 0.5rem 0.75rem;
  font-size: 0.75rem;
  color: $gray-700;
  text-align: right;
  span {
    display: inline-block;
    min-width: 4rem;
    margin-left: 1rem;
  }
  .lines:before {
    content: "lines: ";
  }
  .words:before {
    content: "words: ";
  }
  .characters:before {
    content: "characters: ";
  }
}
